Thirteen Years of Service
Posts: 40
Threads: 2
RE: [Python] Guess your number 02-14-2013, 09:26 AM
#31
(02-14-2013, 09:14 AM)Deque Wrote: (02-14-2013, 12:24 AM)MrBlaise Wrote: Code:
low = 0
high = 100
ans = (high + low)/2
print("Please think of a number between 0 and 100!")
while True:
print("Is your secret number "+str(ans)+"?")
x = raw_input("Enter 'h' to indicate the guess istoo high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate I guessed correctly. ")
if x == 'h':
high = ans
elif x == 'l':
low = ans
elif x == 'c':
break
else:
print("Sorry, I did not understand your input.")
ans = (high + low)/2
print("Game over. Your secret number was: "+str(ans))
I think this is a good way as well, instead of random I use bisection search, - in this program I do not count the tries but I can write it in if it's necessary - . I'm a newbie so correct me if I'm wrong, but using this method will get the guessed number faster sometimes.
You are right, that this is a faster approach, but it is also less interesting for the user, because the way of the computer's guessing will be determined.
Yes that is very true

The other one is more varied, this is just a raw and monotone solution. I just thought I'd show this approach as well.
Thanks for the reply
I wasn't a hacker for the money, and it wasn't to cause damage.
~ Kevin Mitnick
•
Thirteen Years of Service
Posts: 40
Threads: 2
RE: [Python] Guess your number 02-14-2013, 09:26 AM
#32
(02-14-2013, 09:14 AM)Deque Wrote: (02-14-2013, 12:24 AM)MrBlaise Wrote: Code:
low = 0
high = 100
ans = (high + low)/2
print("Please think of a number between 0 and 100!")
while True:
print("Is your secret number "+str(ans)+"?")
x = raw_input("Enter 'h' to indicate the guess istoo high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate I guessed correctly. ")
if x == 'h':
high = ans
elif x == 'l':
low = ans
elif x == 'c':
break
else:
print("Sorry, I did not understand your input.")
ans = (high + low)/2
print("Game over. Your secret number was: "+str(ans))
I think this is a good way as well, instead of random I use bisection search, - in this program I do not count the tries but I can write it in if it's necessary - . I'm a newbie so correct me if I'm wrong, but using this method will get the guessed number faster sometimes.
You are right, that this is a faster approach, but it is also less interesting for the user, because the way of the computer's guessing will be determined.
Yes that is very true

The other one is more varied, this is just a raw and monotone solution. I just thought I'd show this approach as well.
Thanks for the reply
I wasn't a hacker for the money, and it wasn't to cause damage.
~ Kevin Mitnick
•
Thirteen Years of Service
Posts: 40
Threads: 2
RE: [Python] Guess your number 02-14-2013, 09:26 AM
#33
(02-14-2013, 09:14 AM)Deque Wrote: (02-14-2013, 12:24 AM)MrBlaise Wrote: Code:
low = 0
high = 100
ans = (high + low)/2
print("Please think of a number between 0 and 100!")
while True:
print("Is your secret number "+str(ans)+"?")
x = raw_input("Enter 'h' to indicate the guess istoo high. \
Enter 'l' to indicate the guess is too low. \
Enter 'c' to indicate I guessed correctly. ")
if x == 'h':
high = ans
elif x == 'l':
low = ans
elif x == 'c':
break
else:
print("Sorry, I did not understand your input.")
ans = (high + low)/2
print("Game over. Your secret number was: "+str(ans))
I think this is a good way as well, instead of random I use bisection search, - in this program I do not count the tries but I can write it in if it's necessary - . I'm a newbie so correct me if I'm wrong, but using this method will get the guessed number faster sometimes.
You are right, that this is a faster approach, but it is also less interesting for the user, because the way of the computer's guessing will be determined.
Yes that is very true

The other one is more varied, this is just a raw and monotone solution. I just thought I'd show this approach as well.
Thanks for the reply
I wasn't a hacker for the money, and it wasn't to cause damage.
~ Kevin Mitnick
•