10-6加法运算
print("tell me two numbers and I'll add them.")
try:
a=int(input("first number:"))
b=int(input("second number:"))
c=a+b
except ValueError:
print("you should enter a number.")
else:
print(f"{a} plus {b} is {c}.")
10-7
#练习10-7
while True:
try:
print("\nTell me two numbers and I'll add them.")
a=int(input("first number:"))
b=int(input("second number:"))
c=a+b
except ValueError:
print("You should enter a number.")
else:
print(f"{a} plus {b} is {c}.")
10-7修改后
#第二版
print('enter \'q\' to quit.')
while True:
try:
a=input("give me a number:")#先接受任意类型的输入
if a=='q':#判断输入是否为'q'
break
a=int(a)#输入不为'q'后转化为整数
b=input("give me another number.")
if b=='q':
break
b=int(b)
except ValueError:
print("Sorry, I really needed a number.")
else:
sum_=a+b
print(f"The sum of {str(a)} and {str(b)} is "+str(sum_)+".")