# python计算输入的两个数字，try...except...判断是否输入的是数字，如果是则相加

python计算输入的两个数字，try…except…判断是否输入的是数字，如果是则相加，如果不输入q或者n，则循环一直下去：

print("Enter 'q' to quit\n")
while True:
try:
x = input('Pleast enter a number>>>')
if (x == 'q'):
break
x = float(x)
y = input('Pleast enter another number>>>')
if (y == 'q'):
break
y = float(y)
except ValueError:
print('Please enter a number, not letter')
else:
sum = x+y
print('The sum of these number is '+str(sum))
dicision = input('Do you want to continue?y/n>>>')
if (dicision == 'n')or (dicision=='q'):
break

def main():
print("Enter 'q' to quit\n")
while True:
try:
x = input('Pleast enter a number>>>')
if (x == 'q'):
break
x = float(x)
y = input('Pleast enter another number>>>')
if (y == 'q'):
break
y = float(y)
except ValueError:
print('Please enter a number, not letter')
else:
sum = x+y
print('The sum of these number is '+str(sum))
dicision = input('Do you want to continue?y/n>>>')
if (dicision == 'n')or (dicision=='q'):
break

main()


©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客