-1

我正在为我的计算机编程原理介绍课程做最后的项目,我采用了一个旧项目来从列表中查找项目价格并将其转换为显示 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

这是我正在尝试修复的转换后的代码..

#Start Program

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") 
input = int(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 input == teamName[k]:
        foundTeamFlag = True
        print("The ", input, "final record for 2012-2013 was ", final[k])
if (foundTeamFlag == False):
    print("Oops, check your team name and try again!")

#End Program

我是一个初学者,将我的代码复制到 IDLE 并收到 NFL 代码的此错误:

SyntaxError: multiple statements found while compiling a single statement
4

3 回答 3

3

我假设您使用的是 Python 3.X?这是您做错的事情的清单:

  1. 将用户的输入(字符串)转换为 int:这是行不通的
  2. 将内置“输入”重新分配给变量名
  3. 仅迭代变量“final”的前 5 个元素
  4. 正如其他回答者所指出的,您可能希望在“最终”元组中使用字符串

我会做如下事情(对于python 3.x,替换raw_inputinput):

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!")
于 2013-05-07T14:45:56.050 回答
1

您的定义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 个)。将它们更改为字符串:

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")

另外,永远不要使用像input, list,pass这样的名字作为变量名......

正如评论者指出的那样,这不是主要的,

  • 您要求用户输入团队的名称,但正在将其转换为整数..
  • 您仅迭代元组的前 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!")

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

dic = {'Bengals': '10-6', 'Bills': '6-10', 'Broncos': '13-3','Browns': '5-11',
 'Chargers': '7-9', 'Chiefs': '2-14', 'Colts': '11-5', 'Dolphins': '7-9',
 'Jaguars': '2-14', 'Jets': '6-10', 'Patriots': '12-4', 'Raiders': '4-12',
 'Ravens': '10-6', 'Steelers': '8-8', 'Texans': '12-4', 'Titans': '6-10'}
name = input('Enter the name of the team you want the records of: ')
data = dic.get(name)
if data != None:
    print("The ", name, "final record for 2012-2013 was ", data)
于 2013-05-07T14:43:17.757 回答
0

您正在混合数据类型。例如,在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!")

现在,为了更 Pythonic 的方式来解决这个问题,放弃循环并使用这样的字典:

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")
results = dict(zip(teamName, final))

inp = raw_input("To find your team's final record for last season please type in their name (Ravens, Texans and etc) : ")
if inp in results:
    print("The ", inp, "final record for 2012-2013 was ", results[inp])
else:
    print("Oops, check your team name and try again!")
于 2013-05-07T14:49:03.223 回答