我正在研究我的计算机编程原理入门课程的最后一个项目,我从一个旧项目中查找项目价格并将其转换为显示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
答案 0 :(得分:3)
我假设您使用的是Python 3.X?以下列出了您错误的事情:
我会做如下的事情(对于python 3.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!")
答案 1 :(得分: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
等名称作为变量名...
正如评论者指出的那样,不是主要评论者,
您的最终代码应为:
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)
答案 2 :(得分: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!")