2017-04-14 84 views
-2
Def main(): 
name = raw_input("What is your name?: ") 
age = int(raw_input("How old are you?: ")) 
number = int(raw_input("What is your favorite number?: ")) 
print name , 'will be 100 years old in the year of', (100 - age + 2017) 
main() 

,這讓我回答沒有(,逗號和?你如何擺脫(,逗號,'那是打印在Python 2.7

但是,當我加號。*:

print number * (name , 'will be 100 years old in the year of', (100 - age + 2017)) 

然後它給了我回答有(,逗號,「我真的不明白爲什麼它這樣做。

而且,如果我想這個問題的答案將在不同的線路,例如,

的Python 的Python 的Python

如何使用\ n分隔成這樣?

+0

所以,你這樣做:'打印的名字,「將東西」,other' – ForceBru

+0

我想打印多達用戶的發言輸入你最喜歡的數字。因此,如果我打印數字*名稱,'將在'年(100 - 年齡+ 2017)100歲,那麼我有相同的問題(逗號和'。我真的不明白爲什麼它在做它 – Andy

回答

0

您正在生成result作爲元組,並且print將顯示使用圓括號和逗號來表示它是元組。您需要將它直接傳遞到print聲明(這可能需要很多參數):

print name , 'will be 100 years old in the year of', (100 - age + 2017) 

在Python 3,你也可以解壓縮:

result = name , 'will be 100 years old in the year of', (100 - age + 2017) 
print (*result) 

更妙(不管Python版本)是使用format string syntax

print '{0} will be 100 years old in the year of {1}'.format(name, 100 - age + 2017) 
+1

解包似乎不起作用,因爲它是一個Python 3的功能,它在2.7中產生一個'SyntaxError'。 – ForceBru

+0

@ForceBru:'SyntaxError'來自試圖使用Python 2的'print'語句作爲函數。一個'from __future__ import print_function',這個解包解決方案在Python 2中工作。 –

+0

我想打印出與用戶輸入相同數量的語句,因此如果我打印數字*名稱,'將會是100年',(100 - 年齡+ 2017),然後我得到相同的問題,因爲(,逗號和'。我真的不明白爲什麼它會這樣做 – Andy