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.")
练习
- Go online and find out what Python’s input does.
- Can you find other ways to use it? Try some of the samples you find.
- Write another “form” like this to ask some other questions.
答案
- 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.)
还可以使用
eval(input())
name = input("what's your name?")
print("Nice to meet you, {}.".format(name))