[Tutor] guess my number game
Jan Erik Moström
lists at mostrom.pp.se
Tue May 8 08:04:51 EDT 2018
> Here is the code with the while loop
> import random
> n = (random.randint(0,100))
> g = int(input('Guess my number, 0 to 100, you have 10 chances'))
> c = 0
> while (c < 10):
> g = int(input('Guess my number, 0 to 100, you have 10 chances'))
> c = c + 1
> if (g >= n):
> print('Lower!')
> elif (g <= n):
> print('Higher!')
> elif (g == n):
> break
> if (g == n):
> print('You guess my number! It took you ' + str(c) + ' tries!')
>
>
> Everyone's code just keeps asking for numbers without giving feedback,
> except for the longer code above.
For me it prints out Lower/higher but not for the first time since the
code asks a second time before doing anything with the reply.
Here is a modified version where I kept most of the structure you have.
Note that '>=' and '<=' was changed to '>' and '<'.
import random
n = (random.randint(0,100))
g = int(input('Guess my number, 0 to 100, you have 10 chances'))
c = 0
while (c < 10 and g != n):
if (g > n):
print('Lower!')
elif (g < n):
print('Higher!')
c = c + 1
g = int(input('Guess my number, 0 to 100, you have {}
left'.format(10-c)))
if (g == n):
print('You guess my number! It took you ' + str(c) + ' tries!')
More information about the Tutor
mailing list