while not语句中else后面的任何内容都有缩进问题,并且我确定是因为我的格式不正确。正确的写法是什么?
我试图四处移动语句,但无法弄清
valid_data = False
while not valid_data:
try:
qty = int(input("How many would you like? (1-10)> "))
except Exception as detail:
print("Error: ", detail)
else:
try:
if qty >= 1 and qty <= 10:
valid_data = True
finally:
print("valid entry")
实际结果是将数量设置为1到10之间的用户输入而没有可能的无效输入,例如,如果用户输入字符串,则代码不会崩溃。对于此作业,我必须使用try / except语法。
答案 0 :(得分:0)
您搞砸了缩进:您的finally
语句和下面的print
缺少另外4个缩进空格。如果需要,可以复制并粘贴固定版本:
while not valid_data:
try:
qty = int(input("How many would you like? (1-10)> "))
except Exception as detail:
print("Error: ", detail)
else:
try:
if qty >= 1 and qty <= 10:
valid_data = True
finally:
print("valid entry")