您的位置:首页 > 编程语言 > Python开发

第七天任务 (【基于Python编程从入门到实践】第七章 用户输入和while循环 书本及动手试一试)

2018-01-10 16:17 906 查看

第七章 用户输入和while循环

7.1 函数input( )的工作原理

函数input( ) 让程序暂停运行 让用户输入一些文本 再将这些文本呈现给用户

emmm…



7.1.1 编写清晰的程序

我没查为什么 但是提示说 不是string类型 所以 ‘yc’ 才能成功



+= 运算符 在存储在字符串末尾附加一个字符串



7.1.2 使用int( )来获取数值输入

注意我们赋予变量的类型 不然容易报错



一开始 照搬都错哈哈哈 163约等于64.17英尺



7.1.3 求模运算符

它将两个数相除并返回余数



判断一个数是奇还是偶:





7.1 动手试一试

7.1 汽车租赁

car = input ("Which car do you want?")

print("Let me see if I can find you a " + car + ".")






7-2 餐馆订位

customers = input ("How many people eat?")

customers = int (customers)
if customers > 8:
print ("There's an empty table.")
else:
print ("No table available.")






7-3 10的整数倍

# -*- coding: utf-8 -*

number = input ("输入一个数字:")
number = int (number)

if number % 10 == 0:
print ("该数字是10的整数倍。")
else:
print ("该数字不是10的整数倍。")






7.2 while循环

for循环用于针对集合中的每个元素的一个代码块,而while循环不断地运行 直到指定的条件不满足为止

7.2.1 使用while 循环



7.2.2 让用户选择何时退出





改进





7.2.3 使用标志





7.2.4 使用break退出循环





在任何Python循环中都可使用break语句

7.2.5 在循环中使用continue

这里 不被2 整除的 被打印。只是我把数据改为了20



7.2.6 避免无限循环

小于20都被输出 每一次输出都加一



7.2 动手试一试

7-4 比萨配料

ingredients = "\n Which ingredients do you want?"
ingredients += "\n Enter 'quit' to end the program."

active = True
while  active:
message = input(ingredients)

if message == 'quit' :
active = False
else:
print("\tWe will add " + message.title() + ".")






7-5 电影票

age = input ("How old are you?")

age = int (age)
if age < 3:
print ("Free.")
elif age >= 3 and age < 12:
print ("Please pay 10 dollars.")
else:
print ("Please pay 15 dollars")






7-6 三个出口

哟哟 在7-4 就用了while 和active 所以 我这次改7-5

prompt = "\n How old are you?"
prompt += "\n(Enter 'quit' when you are finished.)"

while True:
age = input (prompt)

if age == 'quit':
break
elif age < 3:
print ("Free.")
elif age >= 3 and age < 12:
print ("Please pay 10 dollars.")
else:
print ("Please pay 15 dollars")






7-7 无限循环



7.3 使用whlie循环来处理列表和字典

7.3.1 在列表之间移动元素



7.3.2 删除包含特定值的所有列表元素



7.3.3 使用用户输入来填充字典





7.3 动手试一试

7-8 熟食店



7-9 五香烟熏牛肉

sandwich_orders = ['one','pastrami','two','pastrami','three','pastrami',]
finished_sandwiches = [ ]
while 'pastrami' in sandwich_orders:
sandwich_orders.remove('pastrami')
print (sandwich_orders)

while sandwich_orders:
orders = sandwich_orders.pop()

print ("I made your " + orders + ".")




7-10



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 编程
相关文章推荐