My students are creating a guess my number game.
They are trying to take this type of code (the flow of it), and turn it
into a code using a while loop.
Here is the first code
n = int(input('Guess my number: '))if (n <= 172 and n >= 174):
print('Correct')elif (n >= 174):
a = int(input('Go Down'))
if (a >= 174):
c = int(input('Go Down'))
if (c >= 174):
g = int(input('Last Guess Go Down'))
if (g == 173):
print('Correct')
elif (c <= 172):
i = int(input('Last Guess Go Up'))
if (i == 173):
print('Correct')
else:
print('Correct')
elif (a <= 172):
e = int(input('Go up'))
if (e >= 174):
f = int(input('Last Guess Go Down'))
if (f == 173):
print('Correct')
elif (e <= 172):
h = int(input('Last Guess Go Up'))
if (h == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')elif (n <= 172):
b = int(input('Go Up'))
if (b >= 174):
d = int(input('Go Down'))
if (d >= 174):
j = int(input('Last Guess Go Down'))
if (j == 173):
print('Correct')
elif (d <= 172):
m = int(input('Last Guess Go Up'))
if (m == 173):
print('Correct')
else:
print('Correct')
elif (b <= 172):
e = int(input('Go Up'))
if (e >= 174):
k = int(input('Last Guess Go Down'))
if (k == 173):
print('Correct')
elif (e <= 172):
l = int(input('Last Guess Go Up'))
if (l == 173):
print('Correct')
else:
print('Correct')
else:
print('Correct')else:
print('Correct')
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.
Can you help us consolidate the code? We are using random.randint.
Thank you!
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor