python-3.x - 在在线编译器中运行良好,但未在终端中运行。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"
任何想法?谢谢
解决方案
你的 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")
推荐阅读
- python - 如何使用 python astropy 将 arcsec 转换为 Mpc?
- ng-zorro-antd - 如何使用角度 css 组件设置 ng-zorro 组件的样式?
- python - 尝试使用 pyomo 和 bonmin 求解模型。模型未正确传递给求解器
- javascript - 使用 jquery 更改 Articulate Storyline 文本颜色?
- sql - 如何使用顶部选择行来定义一个范围,比如 40,000 - 45000 之间?
- html - 如何添加跨度以选择选项元素以在每个选项中显示一个标志?
- webpack - Webpack 更改模块 id
- java - 带有 Serenity 的 Spring 参数化 JUnit 测试
- xcode - 使用 Xcode 和 Fastlane 在 Azure DevOps Pipelines 中对 iOS 应用程序进行代码签名时遇到问题
- bash - Linux命令使用正则表达式将多行合并为单行