python-用户输入和while循环

一、函数input() 的工作原理

函数input() 让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在一个变量中,以方便你使用。

car=input("what kind of car would you like to rent? ")
print("let me see if I can find you a "+car)

people=input("\nHow many people are eating? ")
people=int(people)     ##这里输入的数为字符型,需转化为整形才可比较大小
if people>8:
    print("There is no available table")
else:
    print("There is available table")

处理数值信息时,求模运算符 (%)是一个很有用的工具,它将两个数相除并返回余数:

>>> 4 % 3

1

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "\nWhat is your first name? "      ##这个示例演示了一种创建多行字符串的方式。
name = input(prompt)

二、while循环:

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

可使用while 循环让程序在用户愿意时不断地运行。

prompt="What pizza toppings you need"
prompt+="\nEnter 'quit' to end the program."
message=True
while message:
    ingredient=input(prompt)
    if ingredient =='quit':
        message=False
    else:
        print("We will add "+ingredient+" to the pizza\n")

        在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志 ,充当了程序的交通信号灯。你可让程序在标志 为True 时继续运行,并在任何事件导致标志的值为False 时让程序停止运行。这样,在while 语句中就只需检查一个条件——标志的当前值是否为True ,并将所有测试(是 否发生了应将标志设置为False 的事件)都放在其他地方,从而让程序变得更为整洁。

prompt="How old are you?"
prompt+="\nEnter 'quit' when you finished "
while True:
    age=input(prompt)
    
    if age == 'quit':
        break
    else:
        age=int(age)
        if age<3:
            print("\tfree")
        elif (age>=3 and age<=12):
            print("\tticket:10 dollar")
        else:
            print("\tticket:15 dollar")

输出为:

How old are you?
Enter 'quit' when you finished 2
        free
How old are you?
Enter 'quit' when you finished 5
        ticket:10 dollar
How old are you?
Enter 'quit' when you finished 20
        ticket:15 dollar
How old are you?
Enter 'quit' when you finished quit

------------------
(program exited with code: 0)

        要立即退出while 循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break 语句。break 语句用于控制程序流程,可使用它来控制哪些代码行将执行, 哪些代码行不执行,从而让程序按你的要求执行你要执行的代码。

current_number = 0
while current_number < 10: 
    current_number += 1
    if current_number % 2 == 0:
        continue
    else:
        print(current_number)

                输出为  1 3 5 7 9

        要返回到循环开头,并根据条件测试结果决定是否继续执行循环,可使用continue 语句,它不像break 语句那样不再执行余下的代码并退出整个循环

避免无限循环:如果程序陷入无限循环,可按Ctrl+ C,也可关闭显示程序输出的终端窗口。

三、使用while 循环来处理列表和字典

for 循环是一种遍历列表的有效方式,但在for 循环中不应修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while 循环。通过 将while 循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

1.在列表之间移动元素

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

sandwich_orders=['pastrami', 'Francesinha', 'pastrami', 'ArepaCroque', 'pastrami']
finished_sandwiches=[]
while sandwich_orders:

    current_sandwich=sandwich_orders.pop()
    print('I made your ' +current_sandwich)
    finished_sandwiches.append(current_sandwich)
print("\nThe following sandwiches have been finished:")
for finished_sandwich in finished_sandwiches:
    print(finished_sandwich)
    
    
while 'pastrami' in sandwich_orders:
    sandwich_orders.remove('pastrami')
for sandwich_order in sandwich_orders:
    print(sandwich_order)

3.使用用户输入来填充字典 :可使用while循环提示用户输入任意数量的信息

vocation={}
active=True
while active:
    name=input("What's you name ")
    place=input("If you could visit one placein the world, where would you go? ")
    vocation[name] = place
    
    repeat = input("Would you like to let another person respond? (yes/ no) ")
    if repeat == 'no':
        active= False
for name,place in vocation.items():
    print(name+' wants to go '+place)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值