Итак, я некоторое время работаю над этой проблемой угадывания игры, и я остаюсь царапать свой мозг в течение последних 2 часов, пытаясь понять, что не так, но я не могу. Я также попытался найти решение, но я не хочу делать копию &, и я действительно хочу решить мой код.Python-Застрял в программе угадывания игры?
Вот что я был в состоянии получить до сих пор:
start = 0
end = 100
print 'Please think of a number between 0 and 100!'
user = ''
ans = (start + end)/2
while user != 'c':
print ('Is your secret number ' + str((start + end)/2) + '?')
user = raw_input("Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. ")
if user == 'l':
start = ans
elif user == 'h':
end = end - ans
ans = start
print 'Game over. Your secret number was: ' + str((start + end)/2)
Что я делаю неправильно? Edit: Игра должна запустить что-то вроде этого:
Please think of a number between 0 and 100!
Is your secret number 50?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 75?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 87?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 81?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 84?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. h
Is your secret number 82?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. l
Is your secret number 83?
Enter 'h' to indicate the guess is too high. Enter 'l' to indicate the guess is too low. Enter 'c' to indicate I guessed correctly. c
Game over. Your secret number was: 83
И какая проблема у вас с кодом? Что происходит, когда вы запускаете его? для какого-то ввода, какой результат вы ожидаете, и какой результат вы на самом деле получаете? –
тоже, похоже, это в формате python 2, почему вы не используете python3? – Monacraft
Я беру курс Edx на Intro для CS и программирования, и они используют Python 2.x, вот почему. Но для моих собственных проектов я всегда использую Py 3.x. :) –