exercise 11

print ("How old are you?", end = ' ')
age = input ()
print ("How tall are you?", end = " ")
height = input ()
print ("How much do you weight?", end = " ")
weight = input ()

print(f"So, you're {age} old, {height} tall and {weight} heavy.")

练习

  1. Go online and find out what Python’s input does.
  2. Can you find other ways to use it? Try some of the samples you find.
  3. Write another “form” like this to ask some other questions.

答案

  1. input函数直接读取输入;input函数将输入当做字符串看待,返回字符串类型。

例子:

>>> age = input('your age?')
your age?34
>>> print(age,type(age))
34 <class 'str'>
>>> age = int(input('your age?'))
your age?34
>>> print(age,type(age))
34 <class 'int'>

另外在Python2.x中的input()和python3.x的input()是不同的。
以下是stackoverflow的一个答案:

The difference is that raw_input() does not exist in Python 3.x, while input() does. Actually, the old raw_input() has been renamed to input(), and the old input() is gone, but can easily be simulated by using eval(input()). (Remember that eval() is evil, so if try to use safer ways of parsing your input if possible.)

  1. 还可以使用eval(input())

name = input("what's your name?")
print("Nice to meet you, {}.".format(name))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容