我的Python代码出了什么问题?

2024-09-30 14:29:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为我的计算机编程原理介绍课做最后一个项目,我从一个旧的项目中找到一个项目的价格,并转换成显示一个NFL团队的最终分数。在

我的第一个有效代码:

#Start Program

foundItemFlag = False
itemNum = (34, 124, 178, 219, 225) 
price = (3.76, 1.29, 4.78, 2.76, 4.51) 
input = int(input("Enter the Item Number: "))
for k in range (5):
    if input == itemNum[k]:
        foundItemFlag = True
        print("The item number you've chosen is ", input, "and the price is ", price[k])
if (foundItemFlag == False):
    print("Invalid Item Number!")

#End Program

这是我转换后的代码,我正试图修复。。在

^{pr2}$

我是一个初学者,把我的代码复制到IDLE中,收到了NFL代码的以下错误:

SyntaxError: multiple statements found while compiling a single statement

Tags: the项目代码falsenumberinputifis
3条回答

你在混合数据类型。例如,final元组中有数字和字符串。另外,在读取输入时,您需要一个团队名称,这是一个字符串,但将其转换为int。首先,试试这个:

foundTeamFlag = False
teamName = ("Bills", "Dolphins", "Patriots", "Jets", "Ravens", "Bengals", "Browns", "Steelers", "Texans", "Colts", "Jaguars", "Titans", "Broncos", "Chiefs", "Raiders", "Chargers")
final = ("6-10", "7-9", "12-4", "6-10", "10-6", "10-6", "5-11", "8-8", "12-4", "11-5", "2-14", "6-10", "13-3", "2-14", "4-12", "7-9")
inp = raw_input("To find your team's final record for last season please type in their name (Ravens, Texans and etc) : ")

for k in range(5):
    if inp == teamName[k]:
        foundTeamFlag = True
        print("The ", inp, "final record for 2012-2013 was ", final[k])
        break

if foundTeamFlag == False:
    print("Oops, check your team name and try again!")

现在,要想找到一种更具Python式的方法来解决这个问题,请抛弃循环,使用类似这样的字典:

^{pr2}$

我假设您使用的是python3.X?以下是你做错的事情:

  1. 将用户的输入(字符串)转换为int:这行不通
  2. 将内置“input”重新指派给变量名
  3. 只迭代变量“final”的前5个元素
  4. 正如另一个答案者指出的,您可能希望在“final”元组中使用字符串

我将执行以下操作(对于python3.x,将raw_input替换为input):

foundTeamFlag = False
teamName = ("Bills", "Dolphins", "Patriots", "Jets", "Ravens", "Bengals", "Browns", "Steelers", "Texans", "Colts", "Jaguars", "Titans", "Broncos", "Chiefs", "Raiders", "Chargers") 
finalScores = ("6-10", "7-9", "12-4", "6-10", "10-6", "10-6", "5-11", "8-8", "12-4", "11-5", "2-14", "6-10", "13-3", "2-14", "4-12", "7-9") 
userInput = raw_input("To find your team's final record for last season please type in their name (Ravens, Texans and etc) :")
for name, score in zip(teamName, finalScores):
    if userInput == name:
        foundTeamFlag = True
        print("The ", userInput, "final record for 2012-2013 was ", score)
        break
if (foundTeamFlag == False):
    print("Oops, check your team name and try again!")

你对final的定义有一个问题:

final = (6-10, 7-9, "12-4", "6-10", "10-6", "10-6", "5-11", "8-8", "12-4", "11-5", "2-14", "6-10", "13-3", "2-14", "4-12", "7-9")

这里有减法语句(前2个)。将它们改为字符串:

^{pr2}$

另外,不要使用inputlistpass等名称作为变量名。。。在

正如评论人士指出的,这不是主要原因

  • 您要求用户输入团队名称,但将其转换为整数。。在
  • 您只迭代元组的前5个元素。。。在

您的最终代码应该是:

foundTeamFlag = False
teamName = ("Bills", "Dolphins", "Patriots", "Jets", "Ravens", "Bengals", "Browns", "Steelers", "Texans", "Colts", "Jaguars", "Titans", "Broncos", "Chiefs", "Raiders", "Chargers") 
teams = ("6-10", "7-9", "12-4", "6-10", "10-6", "10-6", "5-11", "8-8", "12-4", "11-5", "2-14", "6-10", "13-3", "2-14", "4-12", "7-9") 
inp = input("To find your team's final record for last season please type in their name (Ravens, Texans and etc) : ")
for k in range(len(teams)):
    if inp == teamName[k]:
        foundTeamFlag = True
        print("The ", input, "final record for 2012-2013 was ", teams[k])
if (foundTeamFlag == False):
    print("Oops, check your team name and try again!")

但更灵活的方法是使用字典:

^{4}$

相关问题 更多 >