zoukankan      html  css  js  c++  java
  • 读书笔记「Python编程:从入门到实践」_7.用户输入和while循环

    7.1 函数input()的工作原理

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

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

    7.1.1 编写清晰的程序

      通过在提示末尾(这里是冒号后面)包含一个空格,可将提示与用户输入分开,让用户清楚地知道其输入始于何处

      有时候,提示可能超过一行,例如,你可能需要指出获取特定输入的原因。在这种情况下,可将提示存储在一个变量中,再将该变量传递给函数input() 。

    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? chang
    
    Hello, chang!

    7.1.2 使用int()来获取数值输入

      函数int() 将数字的字符串表示转换为数值表示

    7.1.3 求模运算符

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

    number = input("请输入一个数字,我将告诉您这个是奇数还是偶数: ")
    number = int(number)
    if number%2 == 0:
        print("您输入的是偶数")
    else:
        print("您输入的是奇数")
    请输入一个数字,我将告诉您这个是奇数还是偶数: 123
    您输入的是奇数
    

    7.1.4 在Python 2.7中获取输入

      如果你使用的是Python 2.7,应使用函数raw_input() 来提示用户输入。这个函数与Python 3中的input() 一样,也将输入解读为字符串。

      Python 2.7也包含函数input() ,但它将用户输入解读为Python代码,并尝试运行它们。因此,最好的结果是出现错误,指出Python不明白输入的代码;而最糟的结果是,将运行你原本无意运行的代码。

      如果你使用的是Python 2.7,请使用raw_input() 而不是input() 来获取输入。

    7.2 while循环简介
    7.2.1 使用while循环
    7.2.2 让用户选择何时退出

      中定义了一个退出值,只要用户输入的不是这个值,程序就接着运行
    7.2.3 使用标志

      定义一个变量,用于判断整个程序是否处于活动状态。这个变量被称为标志
    7.2.4 使用break退出循环

      要立即退出while 循环,不再运行循环中余下的代码,也不管条件测试的结果如何,可使用break 语句。
    7.2.5 在循环中使用continue

      continue 语句,让Python忽略余下的代码,并返回到循环的开头。

    7.2.6 避免无限循环

    active = True
    while active:
        message = input("请输入你的姓名:")
        if message == "quit":
            break
        elif message == "continue":
            continue
        else:
            print("你好," + message)
    请输入你的姓名:chang
    你好,chang
    请输入你的姓名:continue
    请输入你的姓名:li
    你好,li
    请输入你的姓名:quit
    
    Process finished with exit code 0
    

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

      for 循环是一种遍历列表的有效方式,但在for 循环中不应修改列表,否则将导致Python难以跟踪其中的元素。要在遍历列表的同时对其进行修改,可使用while 循环。

      通过将while 循环同列表和字典结合起来使用,可收集、存储并组织大量输入,供以后查看和显示。

    7.3.1 在列表之间移动元素  

    checkList = ["aaa","bbb","ccc"]
    checkedList = []
    
    while checkList:
        item = checkList.pop()
        print(item + ":checked")
        checkedList.append(item)
    for item in checkedList:
        print(item.title())
    ccc:checked
    bbb:checked
    aaa:checked
    Ccc
    Bbb
    Aaa
    

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

    checkList = ["aaa","bbb","ccc","aaa","bbb","ccc"]
    print(checkList)
    while "bbb" in checkList:
        checkList.remove("bbb")
    print(checkList)
    ['aaa', 'bbb', 'ccc', 'aaa', 'bbb', 'ccc']
    ['aaa', 'ccc', 'aaa', 'ccc']
    

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

    responses = {}
    active = True
    while active:
        name = input("请输入您的姓名:")
        response = input("请输入您喜欢的颜色:")
        responses[name] = response
        repeat = input("您还想介绍其他人回答吗(yes/no)")
        if repeat == "no" :
            active = False
    print(responses)
    for name,response in responses.items():
        print(name + "喜欢的颜色是:" + response)
    请输入您的姓名:chang
    请输入您喜欢的颜色:yellow
    您还想介绍其他人回答吗(yes/no)yes
    请输入您的姓名:li
    请输入您喜欢的颜色:red
    您还想介绍其他人回答吗(yes/no)yes
    请输入您的姓名:wang
    请输入您喜欢的颜色:blue
    您还想介绍其他人回答吗(yes/no)no
    {'chang': 'yellow', 'li': 'red', 'wang': 'blue'}
    chang喜欢的颜色是:yellow
    li喜欢的颜色是:red
    wang喜欢的颜色是:blue
    

      

  • 相关阅读:
    D. Babaei and Birthday Cake--- Codeforces Round #343 (Div. 2)
    Vijos P1389婚礼上的小杉
    AIM Tech Round (Div. 2) C. Graph and String
    HDU 5627Clarke and MST
    bzoj 3332 旧试题
    codeforces 842C Ilya And The Tree
    codesforces 671D Roads in Yusland
    Travelling
    codeforces 606C Sorting Railway Cars
    codeforces 651C Watchmen
  • 原文地址:https://www.cnblogs.com/changxinblog/p/9790599.html
Copyright © 2011-2022 走看看