2017-08-15 53 views
0
user_input = ("Enter a number and I will tell you the sum of the multiples of 3 and 5 leading up to it.") 
number = int(user_input) 

我只是試圖從用戶獲得輸入並對其進行某些測試。例如,如果number> 0 :.爲了做到這一點,我假定用戶輸入數據被自動存儲爲一個字符串,並且爲了解決這個問題,我們可以執行代碼框中提供的內容。但是,在執行此操作時,我得到ValueError:int()的基數爲10的無效字面值:'輸入一個數字,我將告訴您3和5的倍數總和。很明顯,我做錯了。有人可以闡明如何正確地做到這一點?python-無法將用戶輸入轉換爲整數格式

- 預先感謝您:)

回答

3

您實際上並未提示您輸入號碼。您示例中的user_input是字符串文字Enter a number and I will tell you the sum of the multiples of 3 and 5 leading up to it.,它不能轉換爲數字。

用戶輸入可以用input()提示。

user_input = input("Enter a number and I will tell you the sum of the multiples of 3 and 5 leading up to it.") 
number = int(user_input) 
+0

Hi @AlpineXJedi如果已解決您的問題,請點擊複選標記考慮[接受它](https://meta.stackexchange.com/q/5234/179419)。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – sauerburger