我有一些代码,用于我们必须在学校完成的任务。我正在尝试在列表中添加一些数字,以便我可以计算该数字的平均值。每当我尝试这样做时,它只会告诉我它是 int 和 str 不受支持的操作数类型。我认为我必须做的是将输入转换为 float 而不是字符串,但我不知道该怎么做。代码如下:
final = False
while final == False:
judgeScoreForLoop = 0
judgeScoreLoop = []
while True:
try:
eventName = str(input("What is the event's name? "))
numberJudges = int(input("How many judges are there? "))
competitorName = str(input("What is the competitor's name? "))
for judgeScoreForLoop in range (0, numberJudges):
judgeScore = input("Enter the judge score here: ")
judgeScoreLoop.append(judgeScore)
judgeScoreForLoop + 1
break
except ValueError:
print("One of the inputs was invalid, please try again.")
finalJudges = numberJudges - 2
judgeScoreCombined = sum(judgeScoreLoop)
judgeFinalScore = judgeScoreCombined / finalJudges
if competitorName == "Finish".lower():
final = True
print(judgeFinalScore)
最佳答案
您可以将输入直接转换为 float
judgeScore = float(input("Enter the judge score here: "))
但最好先检查用户是否提供了号码 prehaps 使用 isdigit() 我不想透露太多,但这应该有帮助
关于python - 编程新手并试图完成学校的这项任务,我似乎无法将列表中的数字添加在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37758812/