Python - 值的语法错误

时间:2014-04-10 10:22:54

标签: python syntax

我正在尝试用Python创建一个获得用户价格和折扣的程序。如果discountCode为1或2,则应从原始成本中扣除10%。但是,当我尝试执行它时,代码似乎不起作用。我试过< = 1或> = 2和1,2但似乎没有任何效果,并返回语法错误。我在这里看到了什么吗?

#Pseudocode

#User inputPrice and discountCode
#If discountCode = 1 or 2:
#   outputPrice = (0.1 * inputPrice) - inputPrice
#else:
#   outputPrice = inputPrice
#print (outputPrice)

inputPrice = input ("What is the price of the product?\n")
discountCode = input ("What is your discount code?\n")

if discountCode 1,2:
    outputPrice = (0.1 * inputPrice) - inputPrice
elif discountCode not 1,2:
    outputPrice = inputPrice

print ("Your final total comes to\t", outputPrice)

2 个答案:

答案 0 :(得分:2)

您应该将输入转换为int并修复in语句:

inputPrice = int(input ("What is the price of the product?\n"))
discountCode = int(input ("What is your discount code?\n"))

if discountCode in (1,2):
    outputPrice = (0.1 * inputPrice) - inputPrice
elif discountCode not in (1,2):
    outputPrice = inputPrice

print ("Your final total comes to\t", outputPrice)

答案 1 :(得分:2)

添加in运算符并用括号括起1, 2

inputPrice = int(input("What is the price of the product?\n"))
discountCode = int(input("What is your discount code?\n"))

if discountCode in (1, 2):
    outputPrice = (0.1 * inputPrice) - inputPrice
elif discountCode not in (1, 2):
    outputPrice = inputPrice

print ("Your final total comes to\t", outputPrice)

您实际上并不需要if ... elif声明:

inputPrice = int(input("What is the price of the product?\n"))
discountCode = int(input("What is your discount code?\n"))

if discountCode in (1, 2):
    outputPrice = (0.1 * inputPrice) - inputPrice
else:
    outputPrice = inputPrice

print ("Your final total comes to\t", outputPrice)