while RUN2:
chose = int(input('Type the option number from 1 to 6:'))
if chose == 1:
print(grossPayMsg())
print("Your gross pay is", grosspay(workhour, hourlywage))
elif chose == 2:
print(withHoldingsMsg())
print("Your withholdings is", withhd(workhour, hourlywage))
elif chose == 3:
print(federalTaxMsg())
print("Your Federal Tax is", fedtax(workhour, hourlywage))
elif chose == 4:
print(marylandTaxMsg())
print("Your Federal Tax is", mdtax(workhour, hourlywage))
elif chose == 5:
print(netPayMsg())
print("Your Federal Tax is", net(workhour, hourlywage))
elif chose == 6:
print ("Thank you for using. Goodbye!")
RUN2 = False
else:
print("/////// Invalid option number ///////")
=============================================== =======================
问即可。如果输入不输入选项号,我怎么能避免像下面的ValueErrors?我知道有些用户可以故意或偶然命中输入..是否有任何声明可以避免这种情况?我只学习了int(输入)或输入......
ValueError:无法将字符串转换为float
ValueError:基数为10的int()的文字无效:''
=====================================
提前非常感谢....
答案 0 :(得分:1)
您可以通过更改此代码
删除none
if chose == 1:
print(grossPayMsg())
print("Your gross pay is", grosspay(workhour, hourlywage))
到
if chose == 1:
grossPayMsg() # print is removed because when you use print it prints the return value of grossPayMsg()
print("Your gross pay is", grosspay(workhour, hourlywage))
您可以使用strng.isnumeric()
函数检查输入的字符串是否为数字。之后转换为float
并将其存储在workhour
和hourlywage
strng = input("What's your hours worked?:")
if strng.isnumeric() :
workhour = float(strng)