2013-05-07 95 views
-1

我正在致力于我的计算机程序设计原理课程的最终项目,我花了一个旧项目从列表中找到项目价格并将其转换为显示NFL团队的最终分数。我的Python代码有什么问题?

我的作品第一次代码:

#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 
+3

首先,避免使用一个变量名与方法名称相同。在这种情况下,'input'被用作方法和数据变量。 – BlackVegetable 2013-05-07 14:42:41

+0

我没有得到'SyntaxError'。然而,在你询问团队名称然后尝试将其转换为整数时出现错误。 – mgilson 2013-05-07 14:43:04

+4

您还应该了解字典。而不是两个元组,你可以做一个字典:'final'= {'Bills':'6-10','Dolphins':'7-9'}'然后,'print finals ['Bills']'会打印' 6-10',无需搜索(使用循环)或将名称转换为整数。 – askewchan 2013-05-07 14:45:46

回答

3

我假设你正在使用Python 3.X?这里的事情的清单,你正在做的错:

  1. 转换用户的输入(串)为int:这是行不通的
  2. 重新分配内置的“输入”变量名
  3. 只有遍历变量的第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!") 
+0

'final'不是Python中的内建。 – 2013-05-07 14:49:13

+0

@HenryKeiter你是对的,我的坏。编辑 – Dhara 2013-05-07 14:52:32

+0

好吧,所以最好不要把输入作为一个变量本身?总是添加到它?我现在使用googling tuple,还没有听说过那个词呢! – 2013-05-07 15:44:59

1

您的final的定义是一个概率lem:

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

而且,从来没有使用的名称,如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!") 

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

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) 
+0

它究竟有什么问题? – mgilson 2013-05-07 14:44:00

+0

等等,有什么不对?那个元组?我的意思是,我意识到他可能不打算将字符串与字符串混合在一起,但不应该该元组不会破坏代码? – BlackVegetable 2013-05-07 14:44:18

+0

您说的是代码中的错误是正确的,但是, t导致SyntaxError – mgilson 2013-05-07 14:45:50

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

现在,一个更Python的方式来解决这个问题,沟循环,使用这样的解释:

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