我该如何处理从真实到真实的部分?我需要一个异常,告诉用户他需要输入一个数字(以防他意外键入一个字符串)或一个值>;0.我尝试将nyear转换为int,这样会引发值错误,但这只会导致错误
你们怎么处理
def main():
nyear = int(raw_input('Enter the years: '))
i = 0
while True:
try:
intTarget = int(nyear)
except ValueError:
print 'Value needs to be a number and needs to be greater than 0'
nyear = int(raw_input('Enter the years: '))
for year in range(nyear):
for month in range(12):
rinch = float(raw_input('How many inches of rain: '))
i += rinch
total = i
tmonths = (month + 1) * (year + 1)
ravg = total/tmonths
print total, tmonths, ravg
main()
raw_input
语句移到try
块中李>break
关键字来中断while
循环李>string
转换为int
。如果在类型转换过程中出现任何异常,请再次询问。这将进入代码的异常部分李>if
循环检查enter number是否大于0李>使用 e、 g
输出:
相关问题 更多 >
编程相关推荐