0
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 个整数组成的数组,然后计算最小值、最大值和其他一些东西。我希望用户能够输入“70 71 72 73”之类的四个分数,然后将这四个分数存储为四个整数的数组(列表?)。

谢谢你的帮助!

4

2 回答 2

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)
于 2013-03-01T17:29:02.323 回答
0

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

print "Your golf scores are: ", Scores

或者

print "Your golf scores are: "+ str(Scores)
于 2013-03-01T17:39:48.030 回答