打印订单+总成本

2024-10-17 02:31:27 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我对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')

如果有人能给我一些提示来改进函数并使代码正常工作,那肯定会有帮助


Tags: 代码youyourifisordernowtotal
2条回答

我假设您的代码中还有一些其他问题,格式与您发布的相同,并且您的脚本似乎没有输出if语句中提供的答案

发生这种情况的原因是,在有效答案的情况下,您指定了sandChoice = str(sandChoice),从而将其转换为字符串。但是,在if语句中,您将sandChoice与整数进行比较

记住'1'不等于1

要解决这个问题,可以设置sandChoice = int(sandChoice)或与if语句中的字符串进行比较,如:if (sandChoice == '1'):

在下面找到您发布的全部代码的工作版本

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 = int(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')

我大致了解了你想要的程序。我稍微修改了你的代码

这里更新了代码

order = ("**** YOUR ORDER INCLUDES \n ")
totalCost = 0.00
comboStatus = 0

while True:
    print("Select your sandwich!")
    print("[1] Chicken sandwich - $5.25")
    print("[2] Tofu - $5.75")
    print("[3] Steak - $6.25")
    print("[4] Exit")
    sandChoice = input("Enter choice: ")

    if sandChoice not in ('1', '2', '3', '4'):
       print("Your response is invalid. Try again.")
       continue

    sandChoice = int(sandChoice)

    if (sandChoice == 1):
        print("You chose Chicken!")
        totalCost += 5.25
        print("Your total is now $", totalCost)
        order += ("CHICKEN SANDWICH  \n")
        comboStatus += 1
    elif (sandChoice == 2):
        print("You chose Tofu!")
        totalCost += 5.75
        print("Your total is now $", totalCost)
        order += ("TOFU SANDWICH \n")
        comboStatus += 1
    elif (sandChoice == 3):
        print("You chose Steak!")
        totalCost += 6.25
        print("Your total is now $", totalCost)
        order += ("STEAK SANDWICH \n")
        comboStatus += 1
    elif (sandChoice == 4):
        break

print('The total cost of your order is $' + str(totalCost) + '. \n')

相关问题 更多 >