因此,我对Python还是相当陌生的,但是我遇到了很大的麻烦,因为随着代码的运行,它不再打印出总数。因此,在询问三明治并输入三个选项中的一个后,直接进入下一个问题。我不明白它为什么会这样。。。但是我的代码的开头是这样的
order = ("**** YOUR ORDER INCLUDES \n ")
totalCost = 0.00
comboStatus = 0
这基本上就是三明治、饮料和薯条的每个部分的样子
while true:
sandChoice = input("Select your sandwich! Please enter 1, 2, or 3.")
if sandChoice not in ('1', '2', '3'):
print("Your response is invalid. Try again.")
else:
sandChoice = str(sandChoice)
break
if (sandChoice == 1):
print("You chose Chicken!")
totalCost += 5.25
print("Your total is now $" + str(totalCost))
order += ("CHICKEN SANDWICH \n")
comboStatus += 1
if (sandChoice == 2):
print("You chose Tofu!")
totalCost += 5.75
print("Your total is now $" + str(totalCost))
order += ("TOFU SANDWICH \n")
comboStatus += 1
if (sandChoice == 3):
print("You chose Steak!")
totalCost += 6.25
print("Your total is now $" + str(totalCost,2))
order += ("STEAK SANDWICH \n")
comboStatus += 1
然而,最后,这就是我的代码的样子
print('The total cost of your order is $' + str(totalCost) + '. \n')
如果有人能给我一些提示来改进函数并使代码正常工作,那肯定会有帮助
我假设您的代码中还有一些其他问题,格式与您发布的相同,并且您的脚本似乎没有输出if语句中提供的答案
发生这种情况的原因是,在有效答案的情况下,您指定了
sandChoice = str(sandChoice)
,从而将其转换为字符串。但是,在if语句中,您将sandChoice与整数进行比较记住
'1'
不等于1
要解决这个问题,可以设置
sandChoice = int(sandChoice)
或与if语句中的字符串进行比较,如:if (sandChoice == '1'):
在下面找到您发布的全部代码的工作版本
我大致了解了你想要的程序。我稍微修改了你的代码
这里更新了代码
相关问题 更多 >
编程相关推荐