python - try:/except: 尝试运行模块时在 Python 中返回语法错误
问题描述
下面是我的代码,错误在第 4 行“except”给出了一个语法错误......(这是一个调试和错误捕获分配。我看不出它有什么问题)
while True:
try:
userInputOne = input(int("How much time in hours a week, do you spend practicing? ")
except TypeError:
print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
break
else:
userInputTwo = str(input"How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
print("Please use one of the options. ")
else:
print("Let's calculate...")
break
解决方案
我附上了工作代码。语法错误是由缺少括号和错误的缩进引起的。看看你的else:
陈述。它与声明的高度不同try:
。TypeError 意味着,您不必将输入转换为字符串,因为它们已经是。否则我建议你创建一些变量并int()
在你想用它们计算时转换它们。
while True:
try:
userInputOne = input("How much time in hours a week, do you spend practicing? ")
except TypeError:
print("Oops! Practice time must be rounded to the nearest integer. It also needs to be a numerical value! ")
break
else:
userInputTwo = input("How good to do want to be? Enter 'very good', 'good', mediocre, 'not good' ")
if userInputTwo not in ('very good', 'good', 'mediocre', 'not good'):
print("Please use one of the options. ")
else:
print("Let's calculate...")
break
编辑:我建议使用 PyCharm(如果你不这样做),它的自动缩进功能和漂亮的“缩进指南”。所以你可以更容易地看到许多错误。
推荐阅读
- java - 从 java 代理调用方法时获取 IllegalArgumentException
- python - 在python中保存excel文件时如何将日期字符串读取为日期对象?
- sql-server - 我无法从 Visual Studio 2019 登录 Azure 服务
- c++ - 模板实例化点、依赖名称和 ADL
- java - jackosn JSON 格式化程序在没有适当验证的情况下反序列化
- javascript - 如果 Toggle() 类隐藏,则取消选中单选按钮 如果切换类可见,请选中单选按钮
- swift - skpsmtpmessage 发送邮件功能无法正常工作?
- reactjs - 为 Material UI (React) 中的所有容器设置默认的 maxWidth
- javascript - Firebase ID 令牌已过期。从您的客户端应用程序获取新的 ID 令牌并重试(auth/id-token-expired)?
- javascript - ` fetch("/createpost", {...} ` 工作不正常