2016-05-30 25 views
0

我想學習python,並且我遵循關於verson 3的視頻指令並使用最新的Pycharm IDE。我的代碼中出現錯誤,我找不到,初學者程序

我的屏幕看起來像指導員的屏幕,但我可以通過盯着它看太久。他的代碼在我的崩潰時執行完美。我錯過了什麼?

錯誤消息:

line 6, in <module> 
    balance = float(input("OK, ", name, ". Please enter the cost of the ", item, ": ")) 
TypeError: input expected at most 1 arguments, got 5 

程序直到第一部分到線6:

# Get information from user 

print("I'll help you determine how long you will need to save.") 
name = input("What is your name? ") 
item = input("What is it that you are saving up for? ") 
balance = float(input("OK, ", name, ". Please enter the cost of the ", item, ": ")) 

的pycharm版本是:

PyCharm社區版2016年1月4日生成# PC-145.1504,構建於May 25,2016 JRE:1.8.0_77-b03 x86 JVM:Java HotSpot™服務器虛擬機,由 Oracle公司

現在,我只是盲目的或者是有可能在我的版本和教師版本之間有一個小更新已經發生了一個可能的IDE的問題,他正在教蟒蛇3.

非常感謝提前任何人都可以拋出的幫助。

+0

就像錯誤信息所說的那樣,'input'只有一個參數。因此,將大部分內容放入'print'調用中,並將'':''作爲'input'提示符。 –

+0

您可能會在'input()'中將'''用'連接運算符'+'混淆 - 將所有''改爲'+',您應該沒問題。 – Bassem

+0

'輸入預期最多1個參數,得到5''你傳遞5個參數,你應該傳遞1.邁克爾 – njzk2

回答

2

在Python中,input運算符會接受一個輸入(您希望顯示的字符串)。同樣在Python中,字符串連接使用+運算符完成。在你當前的操作中,你傳遞5個單獨的字符串,而不是你想要使用的1個字符串。改變這一行代碼:

balance = float(input("OK, "+ name +". Please enter the cost of the" + item + ": ")) 
0
print ("I'll help you determine how long you will need to save.") 
name = raw_input("What is your name? ") 
item = raw_input("What is it that you are saving up for? ") 
balance = float(raw_input("OK, "+ name +". Please enter the cost of the "+ item +": ")) 
print name 
print item 
print balance 
+0

OP顯然使用了python 3,其中'input'用於獲取一個字符串而不是'raw_input',而'print'函數需要像所有其他函數一樣使用'()'括號 –

0

一點點改寫可以澄清

input_message = "OK, {name}. Please enter the cost of the {item}: ".format(name=name, item=item) 
balance = float(input(input_message)) 

input參數應該只是一個字符串,我所要建造使用formathttps://docs.python.org/2/library/string.html#format-examples

你通過5個對象,說:

  • "OK, "
  • name
  • ". "Please enter the cost of the "
  • item
  • ": "

因此所述TypeError

要考慮到應驗證實際的輸入被轉換成一個浮子,如果我輸入「foobar」作爲輸入,則輸入上面的行會給你一個ValueError,你可以自己檢查。

0

嘗試使用字符串格式運算符%s%是一個保留字符,可以直接放入輸入字符串中。如果可能,跟在%後面的s將該變量格式化爲一個字符串。如果你需要一個整數,只需使用%d。然後列出的變量出現的順序由%

balance = float(input("OK %s. Please enter the cost of the %s: " %(name,item))) 

開頭的字符串中你一定要小心,不要改變整數或除非你想這樣的事情發生,我不建議這樣做在漂浮成字符串一個輸入語句。

+0

謝謝大家!用'+'代替','解決了這個問題。現在真正困擾我的是,這個錯誤如何影響了我正在觀看的講座! –

相關問題