【发布时间】:2017-04-20 17:08:16
【问题描述】:
如果用户在 print(M1) 中输入“否”,我将如何停止运行最后一个打印语句?我希望程序在打印 M1 时完成。我得到错误 由于在 if 块中声明了“TaxCode”,当用户输入“否”时出现“NameError:未定义名称“TaxCode”。 谢谢。
name = input("What is your name? ")
while True:
try:
income = int(input("What is your income? ($) "))
break
except ValueError:
print ("Invalid input\nPlease enter a figure")
continue
else:
break
M1 = "This program cannot determine your tax code. Please use the program for secondary income "
print("Please answer the questions with 'yes' and 'no'")
Q1=input("Do you receive an income tested benefit? ")
while Q1 != "yes" and Q1 != "no":
print("\nPlease enter either 'yes' or 'no'")
Q1=input("Do you recieve an income tested benefit? ")
if Q1=="yes":
Q2=input("Is this tax code for the income tested benefit? ")
while Q2 != "yes" and Q2 != "no":
print("\nPlease enter either 'yes' or 'no'")
Q2=input("Is this tax code for the income tested benefit? ")
if Q2=="yes":
TaxCode = "M"
elif Q2=="no":
print (M1)
print("Thanks for answering the questions.",name,"Your tax code is ",TaxCode)
【问题讨论】: