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.")

練習(xí)

  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函數(shù)直接讀取輸入;input函數(shù)將輸入當(dāng)做字符串看待,返回字符串類型。

例子:

>>> 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的一個(gè)答案:

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))
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容