Python 刚开始编程并试图为学校完成这项任务,我可以';I don’我似乎不会把数字加在一起
我有一点代码,这是为了我们必须完成的学校任务。我试图在列表中添加一些数字,这样我就可以平均这个数字。每当我尝试这样做时,它都会告诉我int和str的操作数类型不受支持。我想我要做的是将输入转换为浮点而不是字符串,但我不知道如何转换。代码如下:Python 刚开始编程并试图为学校完成这项任务,我可以';I don’我似乎不会把数字加在一起,python,Python,我有一点代码,这是为了我们必须完成的学校任务。我试图在列表中添加一些数字,这样我就可以平均这个数字。每当我尝试这样做时,它都会告诉我int和str的操作数类型不受支持。我想我要做的是将输入转换为浮点而不是字符串,但我不知道如何转换。代码如下: final = False while final == False: judgeScoreForLoop = 0 judgeScoreLoop = [] while True: try:
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)
您可以将输入直接转换为浮点
judgeScore = float(input("Enter the judge score here: "))
但最好先检查用户是否提供了数字
使用isdigit()预捕获我不想透露太多,但这应该会有所帮助默认情况下输入是字符串;您需要做的是将
judgeScore
值转换为整数(或浮点数),然后再将它们附加到列表中。另外,请注意,judgeScoreForLoop+1
是没有意义的;您的循环已经在递增它,即使它没有递增,您也从未分配加法的结果,因此它只是被丢弃。请注意,input()
函数在Python 2和Python 3之间是不同的。添加您正在使用的选项将非常有用。前任。