当前位置 博文首页 > 早知晓的博客:【Python 习题总结3】三种出路
本篇博客对应书中7.2节的动手试一试习题,以下主要为我做题后的心得笔记。
该题目主要是熟悉一下7.2节中提到的结束 while 循环的几种方式,我贴一下自己写的代码,仅供参考奥。
方法1
age=input("Please input your age: ")
while age!='quit':
age=int(age)
if age<3:
print("Price: free!!!")
else:
if 3<=age<=12:
print("Price: 10$")
else:
print("Price: 15$")
age=input("Please input your age: ")
方法2
active=True
while active:
age=input("Please input your age: ")
if age=='quit':
active=False
else:
age=int(age)
if age<3:
print("Price: free!!!")
else:
if 3<=age<=12:
print("Price: 10$")
else:
print("Price: 15$")
方法3
while True:
age=input("Please input your age: ")
if age=='quit':
break
else:
age=int(age)
if age<3:
print("Price: free!!!")
else:
if 3<=age<=12:
print("Price: 10$")
else:
print("Price: 15$")
运行结果如下图:
注:
1、程序包含嵌套时,一定要注意缩进!整体缩进的方法:先选中需要缩进的代码块,按 Tab 键即可。
2、用户输入以后,需要利用 int() 将用户输入数值化再比较。否则会报错奥,新手很容易遇到的问题。
如果各位小伙伴有什么问题或者建议的话,欢迎大家评论区交流呀~
希望大家都走在开满鲜花的路上,加油鸭~
版权说明
原文作者:早知晓
博文链接:Click here
转载请注明出处,谢谢合作~