如何使用单行原始输入将多个整数存储在数组中?

时间:2013-03-01 17:24:19

标签: python arrays 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”,然后将这四个分数存储为四个整数的数组(列表?)。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我运行代码时看到的错误是关于输出的字符串格式,而不是读取输入。纠正代码的一种方法如下所示。我更改了字符串+列表错误以使用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)

答案 1 :(得分:0)

您可以使用以下任一格式更改第4行:

print "Your golf scores are: ", Scores

print "Your golf scores are: "+ str(Scores)