私信  •  关注

Ashton

Ashton 最近回复了
5 年前
回复了 Ashton 创建的主题 » 如何在Python中循环特定的代码部分?

@munk建议的while循环结束工作。看起来像这样。

#This program ask you to choose a number between 1 and 20, then tries to guess it.
import random
print('Think of a number between 1 and 20, im going to try and guess it.')
print('If my guess is too high or low, please tell me and ill guess again.')
input()

guess=random.randint(1, 20)
answer=input()
print('Is this too high, too low, or correct?')
print(guess)
answer=input()

#answer=input() at end of if statements is necessary to keep them from repeating
while answer != 'correct' :
    if answer == 'low':
        print('how about now?')
        guess=guess+1
        print(guess)
        answer=input()
    if answer == 'high':
        print('how about now?')
        guess=guess-1
        print(guess)
        answer=input()


 if answer == 'correct':
    print('nice!')
    print()
    print('Press CTRL+C to exit')