您的位置:首页 > 编程语言 > Python开发

Python编程:从入门到实践读书笔记-7 用户输入和while循环

2017-03-30 23:22 756 查看
这本书挺好,目前还没有看不懂的地方,但还是不能闭门造车,得找人交流才可以。

7.1 函数input()的工作原理
     函数input()让程序暂停运行,等待用户输入一些文本。获取用户输入后,Python将其存储在
一个变量中,以方便你使用。
     eg:
          message = input("Tell me something and I  will repeat it back to you: ")
          print(message)
     op:
          Tell me somethin and I will repeat it back to you:python
          python
     注意:1)函数input()接受一个参数:即要向用户显示的提示或说明,让用户知道该如何做。
               2)需要在Tell me somethin and I will repeat it back to you:(用户输入信息)
                    下一行显示用户输入信息
7.1.1 编写清晰的程序
     使用函数input()时,应指定清晰而易于明白的提示,准确地指出你希望用户提供
什么样的信息——指出用户该输入任何信息的提示都行。
     eg:
          name = input("Please enter you name: ")
          print("Hello, " + name + "!")
     op:
          Please enter you name: Chandler
          Hello, Chandler!
     有时候,提示可能超过一行,在这种情况下, 可将提示存储在一个变量中,再将该变量
传递给函数input()。这样,即便提示超过一行,input() 语句也非常清晰。
     eg:
          prompt = "If you tell us who you are, we can personalize the messages you see."
          prompt +="\nWhat is you first name? " + "\n"

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

          Hello, chandler!
          解释:第1行将消息的前半部分存储在变量prompt中;
                    第2行中,运算符+=在存储在prompt中的字符串末尾附加一个字符串。

7.1.2 使用int()来获取数值输入
     使用函数input()时,Python将用户输入解读为字符串。
     eg:
          >>> age = input("How old are you? ")
          How old are you? 21
          >>> age
          '21'
          注意:用户输入的是数字21,但我们请求Python提供变量age的值时,它返回的是'21'——用户输入
                   的数值的字符串表示,因为这个数字用引号括起了。
     eg:
          >>> age = input("How old are you? ")
                  How old are you? 21
          >>> age >= 18
          Traceback (most recent call last):
               File "<stdin>", line 1, in <module>
           TypeError: unorderable types: str() >= int()     
          注意:当我们试图将输入用于数值比较时,Python会引发错误,因为它无法将字符串和整数进行比较
          为解决这个问题,可使用函数int(),它让Python将输入视为数值。
          >>> age = input("How old are you? ")
          How old are you? 21
          >>> age = int(age)
          >>> age >= 18
          True
     情景1:判断一个人是否满足坐过山车的 身高要求。
     eg:
          height = input("How tall are you, in inches? ")
          height = int(height)

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

          You'll be able to ride when you're a little older.

7.1.3 求模运算符(%—>求余数)
     处理数值信息时,求模运算符(%)是一个很有用的工具,它将两个数相除并返回余数:
      eg:
           >>> 4 % 3
           1
           >>> 5 % 3
           2
           >>> 6 % 3
           0
           >>> 7 % 3
           1 
     注意:如果一个数可被另一个数整除,余数就为0,因此求模运算符将返回0。可利用此来判断奇偶数。

7.1.4 在Python 2.7 中获取输入
     Python 2.7—>raw_input()来提示用户输入
     Python 3   —>input()来提示用户输入
     Python 2.7也包含函数input(),但它将用户输入解读为Python代码,并尝试运行它们。

7.1 
     car = input("Which car do you want?")
print("Let me see if I can find you a" + car + ".")

7.2 
     number = input("How many people have dinner?")
     number = int(number)

    if number >= 8:
         print("We do not have an empty desk!")
    else:
         print("We have an empty desk!Follow me!")

7.3
     number =input("Please type a number!")
     number =int(number)
     if number % 2 == 0:
         print("\nThe number " + str(number) + " is even.")
     else:
         print("\nThe number " + str(number) + " is odd.")
     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐