• while循环


    While 循环

    #while 循环
    
    #基本循环
    time = 1
    while time <= 5:
        print(time)
        time += 1
    
    
    #让用户选择何时退出
    方式一
    msg = '
     告诉我一句话,我会重复一次:'
    msg += '
     当你不想玩时,输入 quit 退出程序。'
    message = ''
    while message != 'quit':
        message = input(msg)
        if message != 'quit':
            print(message)
    
    
    #方式二
    #使用标志
    msg = '
     告诉我一句话,我会重复一次:'
    msg += '
     当你不想玩时,输入 quit 退出程序。'
    active = True
    while active:
        message = input(msg)
        if message == 'quit':
            active = False
        else:
            print(message)
    
    #方式三
    #使用break退出循环
    msg = '
     你想去哪个城市:'
    msg += '
     当你不想玩时,输入 quit 退出程序。'
    while True:
        city = input(msg)
        if city == 'quit':
            break
        else:
            print('I want to %s ' % city)
    
    
    #在循环中使用continue
    num = 0
    while num < 10:
        num += 1
        if num % 2 == 0:
            continue    #退出当前循环,进行下一次循环。
        print(num)
    
    
    #编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit' 时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨
    #中添加这种配料。
    
    while True:
        peiliao = input('请输入一种配料:')
        if peiliao == 'quit':
            break
        print('我们会添加 %s 到披萨中!' % peiliao ,'如果您选择好了,输入 quit退出!')
    print('您的披萨做好了!')
    
    # 有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用
    # 户的年龄,并指出其票价。
    # 在while 循环中使用条件测试来结束循环。
    # 使用变量active 来控制循环结束的时机。
    # 使用break 语句在用户输入'quit' 时退出循环。
    
    active = 0
    while active==0:
        age = input('Please input your age:')
        if age == 'quit':
            active +=1
            continue
        age = int(age)
        if age < 3:
            print('Your are free to watch films!')
        elif age < 12:
            print('You have pay $10.')
        elif age >= 12:
            print('You have pay $15.')
        print("If you want to quit,please input 'quit'")
    print('Thank you for using!')
    
    #将一个列表中的元素循环移动到另一个列表
    u_users = ['alice','bran','candace']
    users = []
    while u_users:
        user = u_users.pop()      #每次从末尾删除一个元素
        users.append(user)
    print("u_users = ",u_users)
    print("users = ",users)
    
    #删除列表中包含特定值的所有元素。
    pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    print(pets)
    while 'cat' in pets:
        pets.remove('cat')
    print(pets)
    
    
    #使用用户输入来填充字典
    responses = {}
    active = True
    while active:
        name = input('
    what is your name? ')
        response = input('Which moutain would you like to climb someday? ')
        responses[name] = response
    
        repeat = input('Would you like to let another person respond? (yes/no) ')
        if repeat == 'no':
            active = False
    print(responses)
    for name,response in responses.items():
        print('%s wouldd like to climb %s' % (name,response))
    
    
    # 创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列
    # 表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明
    # 治都制作好后,打印一条消息,将这些三明治列出来。
    
    sandwich_orders = ['Prosciutto','Press','Panini','Reuben']
    finished_sandwiches = []
    while sandwich_orders:
        finished = sandwich_orders.pop()
        print("I made you %s sandwich" % finished)
        finished_sandwiches.append(finished)
    for i in finished_sandwiches:
        print("%s sandwich is done." % i)
    
    
    # 使用为完成练习7-8而创建的列表sandwich_orders ,并确保'pastrami' 在其中至少出现了三次。在程序开头附近添加
    # 这样的代码:打印一条消息,指出熟食店的五香烟熏牛肉卖完了;再使用一个while 循环将列表sandwich_orders 中的'pastrami' 都删除。确认最终的列
    # 表finished_sandwiches 中不包含'pastrami' 。
    
    sandwich_orders = ['Prosciutto','pastrami','Panini','pastrami','Press','Reuben','pastrami']
    print('pastrami is 卖完了.')
    active = True
    while 'pastrami' in sandwich_orders:
        sandwich_orders.remove('pastrami')
    print(sandwich_orders)
    
    
    # 编写一个程序,调查用户梦想的度假胜地。使用类似于“If you could visit one place in the world, where would you go?”的提示,并编写一个打印调查
    # 结果的代码块。
    a = {}
    active = True
    while active:
        name = input('
    What is your name? ')
        places = input('If you could visit one place in the world, where would you go? ')
        a[name] = places
    
        print('Would you like to continue? (yes/no) ')
        b = input()
        if b == 'no':
            active = False
    print(a)
    

      

  • 相关阅读:
    hdoj 1175 (bfs)
    hdoj1072 Nightmare bfs
    hdoj1242(bfs+priority_queue)
    hdoj1242(dfs 剪枝 解法)
    hdoj1421(bfs)
    配置Windows 2008 R2 64位 Odoo 8.0 源码PyCharm开发调试环境
    [转]编译VC++程序warning C4819快速解决
    解决VS2013+IE11调试DevExpress ASP.NET MVC的性能问题
    Google被墙 Android开发工具下载地址
    Mac OS X Yosemite安装盘U盘制作
  • 原文地址:https://www.cnblogs.com/jacky-zhao/p/7994118.html
Copyright © 2020-2023  润新知