巨蟒猜谜游戏:帮助玩家猜中正确答案



你好,我对写我书上的这个练习很困惑,你能帮我吗?到目前为止,我已经写了这么多:

import random
def main():
again = 'y'
number_of_guesses = 0
while again =='y':
print('guess the number game')
print(' ')
print('Guess my number between 1 and 1000 with the fewest guesses: ')

x = random.randrange(1,1000)
guess = int(input())

while guess !='':
if guess < x:
print('Your guess is too low , try again')
guess = int(input())

elif guess > x:
print('Your guess is too high , try again')
guess = int(input())

elif guess == x:
print('Congratulations! You guessed the number')
print('Do you want to repeat?')
print('if you want do, type y, else type the n')
number_of_guesses += 1
again = str(input(''))

break
else:
print('Error/!, your enter is wrong:')
main()

问题的主要文本:适当的帮助玩家"归零"正确答案,然后提示用户下一次猜测。当用户输入正确答案,显示"恭喜"。你猜的数字!",并允许用户选择是否重新播放

你的代码没问题!这是怎么回事?检查缩进,如果仍然错误

我想你已经解决了问题,你到底在哪里需要帮助?而不是写

while guess !+ '':

我会写

while True:

您只有多个缩进错误。修正后的版本如下:

import random
def main():
again = 'y'
number_of_guesses = 0
while again =='y':
print('guess the number game')
print(' ')
print('Guess my number between 1 and 1000 with the fewest guesses: ')
x = random.randrange(1,1000)
guess = int(input())
while guess !='':
if guess < x:
print('Your guess is too low , try again')
guess = int(input())
elif guess > x:
print('Your guess is too high , try again')
guess = int(input())
elif guess == x:
print('Congratulations! You guessed the number')
print('Do you want to repeat?')
print('if you want do, type y, else type the n')
number_of_guesses += 1
again = str(input(''))
break
else:
print('Error/!, your enter is wrong:')
main()

最新更新