Learn ptyon the Hrad Way

exercise 11

2017-12-30  本文已影响0人  不娶名字
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))
上一篇 下一篇

猜你喜欢

热点阅读