zoukankan      html  css  js  c++  java
  • 《python crash course》笔记(4)

    1. 用户输入

      1.1 input()

    message = input("Tell me something, and I will repeat it back to you: ") 
    print(message) 

         input()接受一个参数,用来向用户的显示或说明,然后用户输入内容,以string(字符串)的形式存储到message变量中,并且以         回车结束,下面看一个高级一点的例子

    prompt = "If you tell us who you are, we can personalize the messages you see." 
    prompt += "
    What is your first name? " 
    name = input(prompt) 
    print("
    Hello, " + name + "!") 

         输出为

    If you tell us who you are, we can personalize the messages you see.  
    
    What is your first name? Eric  
    Hello, Eric! 

      1.2 int()

         获取数值输入用Int()而非input()

    height = input("How tall are you, in inches? ") 
    height = int(height) 
    if height >= 36: 
       print("
    You're tall enough to ride!") 
    else: 
        print("
    You'll be able to ride when you're a little older.")

         输出如下

    How tall are you, in inches? 71
    You're tall enough to ride!

    2. while循环

      2.1 简单例子

    current_number = 1
    while current_number <= 5: # 注意加上冒号!
        print(current_number)
        current_number += 1

         输出结果如下

    1
    2
    3
    4
    5

        注意使用while循环一个典型的逻辑错误就是自增表达式的位置问题,如下

    current_number = 1
    while current_number <= 5: # 注意加上冒号!
        current_number += 1
        print(current_number)

        这种情况输出如下

    2
    3
    4
    5
    6

      2.2 让用户选择何时退出

    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program. "
    
    message='' # define a string
    while message != 'quit':
        message = input(prompt)    
        print(message) 

        但这样会把quit也打印出来,改善方法就是加个if语句判断

    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program. "
    
    message='' # define a string
    while message != 'quit': # 注意加冒号(colon)
        message = input(prompt)
        if message != 'quit': # 注意加冒号   
            print(message) 

      2.3 使用标志(flag)

    prompt = "
    Tell me something, and I will repeat it back to you:"
    prompt += "
    Enter 'quit' to end the program. "
      
    active = True # define a flag
    while active:
        message = input(prompt)
        if message == 'quit':
            active = False
        else:
            print(message)

      2.4 使用break

    prompt = "
    Please tell me a city you have visited:"
    prompt += "
    (Enter 'quit' when you are finished.) "
    
    while True:
        city = input(prompt) 
        if city == 'quit':
            break          # 执行break后会立即退出while循环
        else:
            print("I'd love to go to " + city.title() + "!")

     2.5 使用break

    current_number = 0
    while current_number < 10:
        current_number += 1
        if current_number %2 == 0:
            continue      # 执行continue后将忽略循环中剩下的代码而开继续新的循环
        print(current_number)

        输出结果如下

    1
    3
    5
    7
    9

      2.6 使用while循环处理列表和字典

        2.6.1 在列表之间移动元素

    unconfirmed_users=['alice','brian','candace']
    confirmed_users=[]
    while unconfirmed_users:
        current_user=unconfirmed_users.pop()
        print("Verifying user: "+current_user.title())
        confirmed_users.append(current_user)
    print("
    The following users have been confirmed:") 
    for confirmed_user in confirmed_users: 
        print(confirmed_user.title())

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

    pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
    print(pets)
    
    while 'cat' in pets:
        pets.remove('cat')
     
    print(pets)

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

    responses = {}
    
    # Set a flag to indicate that polling is active.
    polling_active = True
    
    while polling_active:
        # Prompt for the person's name and response.
        name = input("
    What is your name? ")
        response = input("Which mountain would you like to climb someday? ")
        
        # Store the response in the dictionary:
        responses[name] = response
        
        # Find out if anyone else is going to take the poll.
        repeat = input("Would you like to let another person respond? (yes/ no) ")
        if repeat == 'no':
            polling_active = False
            
    # Polling is complete. Show the results.
    print("
    --- Poll Results ---")
    for name, response in responses.items():
        print(name + " would like to climb " + response + ".")
  • 相关阅读:
    Vue--爬坑
    小程序--爬坑
    同源策略
    如何更改placeholder属性中文字颜色
    vue 项目上传到码云,push时error: failed to push some refs to 'https://gitee.com/mawenrou/vue_ht.git'
    node服务端口被占用
    webpack配置自动打包重新运行npm run dev出现报错
    解决回调地狱
    Apache Spark
    RAM computer
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13281760.html
Copyright © 2011-2022 走看看