我正在为我的计算机编程原理介绍课做最后一个项目,我从一个旧的项目中找到一个项目的价格,并转换成显示一个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
你在混合数据类型。例如,
final
元组中有数字和字符串。另外,在读取输入时,您需要一个团队名称,这是一个字符串,但将其转换为int
。首先,试试这个:现在,要想找到一种更具Python式的方法来解决这个问题,请抛弃循环,使用类似这样的字典:
^{pr2}$我假设您使用的是python3.X?以下是你做错的事情:
我将执行以下操作(对于python3.x,将
raw_input
替换为input
):你对
final
的定义有一个问题:这里有减法语句(前2个)。将它们改为字符串:
^{pr2}$另外,不要使用
input
、list
、pass
等名称作为变量名。。。在正如评论人士指出的,这不是主要原因
您的最终代码应该是:
但更灵活的方法是使用字典:
^{4}$相关问题 更多 >
编程相关推荐