[英]How do I store multiple integers in an array with a single line raw input?
FirstName = raw_input("Please enter your first name: ")
Scores = map(int, raw_input("Please enter your four golf scores: ").split())
print "Score analysis for %s:" % FirstName
print "Your golf scores are: " + Scores
print "The lowest score is " + min(Scores)
print "The highest score is" +max(Scores)
我正在嘗試將用C ++編寫的基本程序轉換為python,我想輸入4個整數的數組,然后計算min,max和其他一些東西。 我希望用戶能夠輸入四個分數,例如“ 70 71 72 73”,然后將這四個分數存儲為四個整數的數組(列表?)。
謝謝你的幫助!
我在運行代碼時看到的錯誤與輸出的字符串格式有關,而不是輸入的讀取。 糾正代碼的一種方法如下所示。 我將字符串+列表錯誤更改為使用print
語句的逗號。 我將兩個string + int錯誤更改為使用字符串插值。
FirstName = raw_input("Please enter your first name: ")
Scores = map(int, raw_input("Please enter your four golf scores: ").split())
print "Score analysis for %s:" % FirstName
print "Your golf scores are:", Scores
print "The lowest score is %d" % min(Scores)
print "The highest score is %d" % max(Scores)
您可以使用以下兩種格式之一更改第四行:
print "Your golf scores are: ", Scores
要么
print "Your golf scores are: "+ str(Scores)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.