我正在尝试用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)
答案 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)