简体   繁体   English

python代码 - IndentationError:意外缩进

[英]python code - IndentationError: unexpected indent

 secret_word = "apple"

 guess = ""

 guess_count = 0

 guess_limit = 3

 guess_finished = False

 while guess != secret_word and not(guess_finished):

     if guess_count < guess_limit:

         guess = input("Enter a word: ")

         guess_count += 1

     else:`
         guess_finished = True
        

if guess_finished:
    print("You lose")

else:
    print("you win!")

You have an extra space at the beginning of line 2 and it goes til while condition.在第 2 行的开头有一个额外的空格,它会一直持续到 while 条件。 You can just remove all spaces and try again.您可以删除所有空格并重试。 This should work这应该工作

secret_word = "apple"
guess = ""
guess_count = 0
guess_limit = 3
guess_finished = False
while guess != secret_word and not(guess_finished):

     if guess_count < guess_limit:
         guess = input("Enter a word: ")
         guess_count += 1
     else:
         guess_finished = True      

if guess_finished:
    print("You lose")
else:
    print("you win!")

All the indents look normal to me, this looks like a syntax error.所有的缩进对我来说都是正常的,这看起来像是一个语法错误。 I think the ' ` ' char does not belong there after the 'else:' in line 19我认为第 19 行中的 'else:' 之后的 '`' 字符不属于那里

Try this code, it works for me...试试这个代码,它对我有用......

secret_word = "apple"
guess = ""
guess_count = 0
guess_limit = 3
guess_finished = False
while guess != secret_word and not(guess_finished):
    if guess_count < guess_limit:
        guess = input("Enter a word: ")
        guess_count += 1
    else:
        guess_finished = True
    
if guess_finished:  
    print("You lose")
else: 
    print("you win!")

This worked for me这对我有用

secret_word = "apple"

guess = ""

guess_count = 0

guess_limit = 3

guess_finished = False

while guess != secret_word and not(guess_finished):

    if guess_count < guess_limit:

        guess = input("Enter a word: ")

        guess_count += 1

    else:
        guess_finished = True
        

if guess_finished:
    print("You lose")

else:
    print("you win!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM