【Python】python编程从入门到实践学习系列-第7章 | 用户输入和while循环

参考书籍–《python编程从入门到实践》

本章总结

在这里插入图片描述

|动手试一试|

# 7.1汽车租赁
car = input("let me see if I can find you a Subaru:")  #python2.7为raw_input()
print(car)
let me see if I can find you a Subaru:audi
audi
# 7.2 餐馆订位
Order = input("how many people will have dinner:")
order = int(Order)  #input()获得的是字符串,int()将字符串转化为数值类型
if order>8:
    print("there is no table available.")
else:
    print("there are tables available for you.")
    
how many people will have dinner: 2
there are tables available for you.
# 7.3 10的整数倍
number = input("input a number:")
number = int(number)
if number % 10 == 0:  #取模运算,判断整除、奇偶数
    print("是10的整数倍")
else:
    print("不是10的整数倍")
input a number:2
不是10的整数倍
# 7.4 比萨配料
#1、while循环
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."

topping = ""
while topping != 'quit':
    topping = input(ingredient)
    if topping != 'quit':
        print("We will add "+ topping.title() + " into pizza!")
# 7.4 比萨配料
#2、使用标记
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."

active = True
while active:
    topping = input(ingredient)
    if topping == 'quit':
        active = False
    else:
        print("We will add "+ topping.title() + " into pizza!")
# 7.4 比萨配料
#3、break语句
ingredient = "\nplease input a series of pizza toppings:"
ingredient += "\nEnter 'quit' to end."

while True:
    topping = input(ingredient)
    
    if topping == 'quit':
        break
    else:
        print("We will add "+ topping.title() + " into pizza!")
# 7.5 电影票
age = input("\nhow old are you: ")
age = int(age)

if age < 3:
    print("the ticket is free.")
elif age > 12:
    print("the ticket is 15$.")
else:
    print("the ticket is 10$.")
# 7.6 三个出口:用while循环、标记、break语句完成7.4或7.5的练习
# 7.7 无限循环
x=1
while x>5:
    print(x)
#7.8 熟食店
sandwich_orders = ['tuna','pastrami','hamburger','tuna','cheese','pastrami','chicken','pastrami']
finished_sandwiches = []
#正序输出
for sandwich_order in sandwich_orders:
    print("I made your " +sandwich_order+" sandwich")
    finished_sandwiches.append(sandwich_order)
#倒叙输出
#while sandwich_orders:
#    current_sandwich = sandwich_orders.pop()
#    print("I made your " +current_sandwich+" sandwich")
#    finished_sandwiches.append(current_sandwich)

print("\nThe following sandwiches have been finished:")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich)
I made your tuna sandwich
I made your pastrami sandwich
I made your hamburger sandwich
I made your tuna sandwich
I made your cheese sandwich
I made your pastrami sandwich
I made your chicken sandwich
I made your pastrami sandwich

The following sandwiches have been finished:
tuna
pastrami
hamburger
tuna
cheese
pastrami
chicken
pastrami
# 7.9五香烟熏牛肉(pastrami)卖完了,接7.8
sandwich_orders = ['tuna','pastrami','hamburger','tuna','cheese','pastrami','chicken','pastrami']
print("Pastrami sandwich has been sold out.\n")

while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
print(sandwich_orders)
Pastrami sandwich has been sold out.

['tuna', 'hamburger', 'tuna', 'cheese', 'chicken']
# 7.10 梦想的度假胜地
responses = {}
polling_active = True
while polling_active:
        name = input("\nwhat's your name? ")
        place = input("\nif you could visit one place in the world, where would you go? ")

        responses[name] = place

        repeat = input("would you like to let another person join the investigation? (yes/no) ")
        if repeat == 'no':
            polling_active = False

print("\n--- Poll Results ---")
for name, place in responses.items():
    print(name + " would like to visit " + place + ".")
what's your name? LYL

if you could visit one place in the world, where would you go? Florence
would you like to let another person join the investigation? (yes/no) yes

what's your name? QQ

if you could visit one place in the world, where would you go? American
would you like to let another person join the investigation? (yes/no) no

--- Poll Results ---
LYL would like to visit Florence.
QQ would like to visit American.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值