Python基础之Python面面观2

该系列为南京大学课程《用Python玩转数据》学习笔记,主要以思维导图的记录

2.6 递归

2.6

2.7 变量作用域

2.7

拓展1 Python常用标准库函数

t_1

拓展2 异常

t_2

异常编程小练习

编写一个程序,让用户输入苹果个数和单价,然后计算出价格总额。

Enter count: 10

Enter price for each one: 3.5

Pay: 35

运用try-except语句让程序可以处理非数字输入的情况,如果是非数字输入,打印消息并允许用户再次输入,直到输入正确类型值计算出结果后退出。以下是程序的执行结果:

Enter count: 20

Enter price for each one: four

Error, please enter numeric one.

Enter count: twenty

Error, please enter numeric one.

Enter count: 20

Enter price for each one: 4

The price is 80.

1
2
3
4
5
6
7
8
9
if __name__ == '__main__':
while True:
try:
count = eval(input('Please input a count:'))
price = eval(input('Please input the price for each one:'))
pay = count * price
print('The price is {}'.format(pay))
except:
print('Error, please enter numeric one.')