【发布时间】:2018-06-20 07:50:40
【问题描述】:
当我尝试运行我的代码时,它会说:
if again == "y":
^
TabError: 缩进中制表符和空格的使用不一致
我的代码是:
import random
def game():
wrong_guesses = [1,2,3,4,5]
num = random.randint(0,100)
guess = input("Guess a number bewtween 1 and 100, you have five tries ")
def wrong_guess():
wrong_guesses.remove()
print("That is not the number, You have {} guesses left".format(len(wrong_guesses)))
def right_guess():
if guess == num:
print("Congratulations! You guessed the number. You had {} guesses left".format(len(wrong_guesses)))
play_again()
def game_over():
if len(wrong_guesses) == 0:
break
play_again()
def play_again():
again = input("Would you like to play again? [y/n}")
if again == "y":
game()
elif again == "n":
break
game()
【问题讨论】:
-
最简单的解决方案是:不要在 python 代码中使用制表符。在您现有的代码中,将所有制表符替换为 4 个空格。将编辑器配置为在按 Tab 时创建 4 个空格。
-
您还有另一个与缩进相关的问题:最后一个
if语句在函数play_again内。 (必须在外面,根据程序的逻辑。) -
好的,谢谢。为您的帮助,但不是我有不同的问题。它给了我同样的错误信息,但现在页面底部有 game()。
-
我想通了。
标签: python