Python Forum
please Help in my coding assignment
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
please Help in my coding assignment
#1
assignment description: Make a program so that the computer can guess what number the person is thinking of

issue: I'm able to make it so that the code works for numbers 1-10 but not numbers that do not start from 1
I have checked all the values I just think my code is using the wrong formula.
coding issue segment:
while True: 
      count +=1
      mid=((Larger-smaller)//2)+1
      print("is your number",mid,"?")
      guess=input("please put <,>,= based on where you number is from where the number is guessed: ")
      if guess =="<":
          Larger=Larger-mid
      elif guess==">":
           smaller= smaller+mid
      else:
          print("Hooray I got it in ",count," tries!")
          break
buran write Nov-13-2023, 09:57 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
I apologise for posting this without actually thinking this through if people still want the answer I just changed this part
mid=((Larger-smaller)//2)+1
to
mid=((Larger-smaller)//2)+smaller
Reply
#3
I think this does what you want, but I am not too sure exactly what you want!

import random
larger = 100
smaller = 40
count = 0
hads = random.randint(smaller, larger)
print('The random number is {hads}')  
while True:
    count +=1
    mid = ((larger-smaller)//2)
    print('mid = ', mid)
    print(f"is your number {mid + smaller}?")
    guess=input("please put <,>,= based on where you number is from where the number is guessed, or q to quit: ")
    # in case you get stuck in the loop
    if guess == 'q':
        break
    elif guess =="<":
        larger = larger - mid
        print(f'number = {hads} , larger = {larger}, smaller = {smaller}')
    elif guess==">":
        smaller = smaller + mid
        print(f'number = {hads} , larger = {larger}, smaller = {smaller}')
    else:
        print(f"Hooray I got it in {count} tries!")
        break        
Seems to take 6 tries, at least with these values! Must be some sort of arithmetical rule!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help With Coding Assignment sarah_mb_sues 4 3,731 Jul-10-2018, 05:10 PM
Last Post: sarah_mb_sues

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020