首页 > 解决方案 > 在在线编译器中运行良好,但未在终端中运行。PYTHON

问题描述

if flag == True:

    print("congatulations you have guessed number correctly in",7-guesses,"Guesses")

    #if flag is false print sorry

    else:    #error line

        print("Sorry!! you failed to guess number correctly in 7 Guesses")

    #open report.txt in appending mode and write user name and guesses taken

    with open("report.txt", "a") as myfile:

        myfile.write(user_name+"\t"+str(7-guesses)+"\n")

当我在在线编译器上运行它时,它工作正常。但是当我在终端运行这段代码时,是说

"File "Hw6.py", line 59
    else:
       ^

SyntaxError: invalid syntax"

任何想法?谢谢

标签: python-3.x

解决方案


你的 else 没有正确缩进试试

if flag == True:

    print("congatulations you have guessed number correctly in",7-guesses,"Guesses")

    #if flag is false print sorry

else:  
    print("Sorry!! you failed to guess number correctly in 7 Guesses")

        #open report.txt in appending mode and write user name and guesses taken

    with open("report.txt", "a") as myfile:

        myfile.write(user_name+"\t"+str(7-guesses)+"\n")

推荐阅读