zoukankan      html  css  js  c++  java
  • Python:从入门到实践--第七章--用户输入和while循环-练习

    #1.编写一个程序,询问用户要租赁什么样的汽车,并打印。
    car = input("What's kind of cars dou you want to rent?,sir:")
    print('Let me see if I can find you a '+ car)
    
    print('
    ')
    #2.编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空桌;否则指出有空桌
    table = input("尊敬的先生/女士,请问订餐人数是多少?:")
    if int(table) > 8:
        print("非常抱歉,已经没有位置了")
    else:
        print("欢迎光临!")
    
    #3.10的倍数:让用户输入一个数字,并指出这个数字是否是10的倍数。
    Ten_number = input("请输入一个数字:")
    if int(Ten_number) % 10 == 0:
        print('是10的倍数')
    else:
        print('不是10的倍数')
    #1.编写一个程序,提示用户输入一系列的比萨配料,并在用户输入‘quit’时结束循环。
    #每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料
    charger_sheet = 'Please input some dosing you want:'
    charger_sheet += "
    Enter 'quit' to end the program.
    "
    
    while messages != 'quit':
        messages = input(charger_sheet)
        if messages != 'quit':
            print('we will add the '+messages+' to pizza')
    
    
    #2.电影票:有家电影院根据观众的年龄收取不同的票价:不到5岁的观众免费,5-12岁的观众为10美元,超过12岁的观众为15美元。
    #编写一个循环,在其中询问用户的年龄,并指出其票价
    prompt = '
    please enter your age:'
    prompt += "
    Enter 'quit' to exit the loop."
    while True:
        age = input(prompt)
        if age == 'quit':
            break
        elif int(age) <5:
            print('free')
        elif 5 <= int(age) <12:
            print('10 dollars.')
        elif int(age) >= 12:
            print('15 dollars.')
        else:
            print('Error!')
            break
    
    
    #3.在while循环中使用条件测试来结束循环
    #使用变量active来控制循环结束的时机
    #使用break语句在用户输入‘quit’时退出循环
    
    charger_sheet = 'Please input some dosing you want:'
    charger_sheet += "
    Enter 'quit' to end the program.
    "
    
    active = True
    while active:
        messages = input(charger_sheet)
        if messages == 'quit':
            active=False
            break
        else:
            print('we will add the '+messages+' to pizza')
            
            
            
    #1.熟食店:创建一个名为sandwich_orders的列表,其中包含各种三明治的名字。
    #在创建一个名为finished_sandwiches的空列表,遍历列表sandwich_orders,对于其中的每种三明治,都打印一条消息
    #并将其移至列表finished_sandwiches。所有三明治都制作好后打印一条消息,将这些三明治列出来。
    sandwich_orders = ['milk','strawberry','blueberry','banana','pastrami']
    finished_sanwiches = []
    while sandwich_orders:
        current_del = sandwich_orders.pop()
        print('当前制作的三明治:' + current_del)
        finished_sanwiches.append(current_del)
    
    print("
    所有制作号的三明治:")
    for finish_sandwich in finished_sanwiches:
        print(finish_sandwich)
        
    print('
    ')
    #2.牛肉卖完了:使用1中的列表sandwich_orders,并确保‘pastrami’在其中至少出现三次。在程序开头附近添加这样的代码:
    #打印一条消息,指出牛肉买完了;再使用一个while循环将列表sandwich_orders中的‘pastrami’都删除。
    #确认最终的列表finished_sandwiches不包含‘pastrami’
    sandwich_orders = ['milk','pastrami','strawberry','pastrami','blueberry','banana','pastrami']
    print( sandwich_orders)
    print('牛肉卖完了')
    
    while 'pastrami' in sandwich_orders:
        sandwich_orders.remove('pastrami')
    
    print(sandwich_orders)
    
    
    #3.度假胜地:编写一个程序,调查用户梦想的度假胜地。使用类似于‘if you could visit one place in the world, where would you go?’
    #并编写一个打印调查结果的代码块
    places = {}
    active = True
    
    while active:
        name = input("
    what's your name?")
        response = input("If you could visit one place in the world,where would you go?")
        places[name] = response
        
        next_question = input('Can you tell me another interesting things?(yes/no)')
        if next_question == 'no':
            active = False
        
    print("
    The results of researches:")    
    for name,response in places.items():
        print(name + ' would like to ' + response)
        
        
     
  • 相关阅读:
    C. Shaass and Lights 解析(思維、組合)
    D. Binary String To Subsequences(队列)(贪心)
    CodeForces 1384B2. Koa and the Beach (Hard Version)(贪心)
    CodeForces 1384B1. Koa and the Beach (Easy Version)(搜索)
    CodeForces 1384C. String Transformation 1(贪心)(并查集)
    CodeForces 1384A. Common Prefixes
    POJ-2516 Minimum Cost(最小费用最大流)
    POJ3261-Milk Patterns(后缀数组)
    HDU-1300 Pearls(斜率DP)
    HDU-4528 小明系列故事-捉迷藏(BFS)
  • 原文地址:https://www.cnblogs.com/geeker-xjl/p/10635287.html
Copyright © 2011-2022 走看看