【发布时间】:2019-05-08 18:33:04
【问题描述】:
基本上,我试图根据给定的输入在这里得到一个粗略的总数。我不知道为什么我不能让它添加,尽管使用 sum。我尝试将整个事情放入一个while循环并将其定义为sumTotal,但什么也没做。我最终想让它显示总数,说“这是您的未打折总数:(在此处插入总数)。
目前,它正在运行,并且总数弹出为 AAyes。这很有趣,但不是我需要它做的。有什么建议吗?
holidayInn = 120
showBoat = 230
mollyPitcher = 180
rideshare = 20
A = 180
B = 230
C = 120
# Declare variables
rooms = 0
hotel = 0
rideshare = "yes"
MOLLYP = "A"
SHOWB = "B"
HOLIDAY = "C"
# Greet the user
print("Hello, and welcome to our program!" + \
" Follow the prompts below to begin" + \
" planning your vacation.")
# Ask about the number of guests
party = int(input("With how many people will you be traveling?" + \
"(Larger groups may qualify for a discount!): "))
# Display discount
if 5 < party <= 8:
print("Cool! Your selection qualifies for a 10% discount" + \
"that will be applied at the end.")
elif party >= 9:
print("Cool! Your selection qualifies for a 30% discount" + \
"that will be applied at the end.")
elif party < 5:
print("Sorry, your purchase does not qualify for a discount.")
# -----------------------------------------------------------------
# Ask about the number of rooms
rooms = int(input("How many rooms will you be booking? " + \
"(please limit to 10 per transaction): ") )
# Ask about the number of nights
nights = int(input("For how many nights will you be staying? "))
# Display Hotels
print("Here are our available hotels:")
print("A. Holiday Inn: $120/night")
print("B. Showboat: $230/night")
print("C. Molly Pitcher Inn: $180/night")
# Ask which hotel
select1 = input("At which hotel will you be staying? " + \
"(Enter the capital letter that corresponds.)")
# Check validity of first selection
if select1 != MOLLYP and select1 != SHOWB and select1 != HOLIDAY:
print("Error: Enter your selection as a capital letter.")
# Ask about ridesharing
select2 = input("Will you be ultizing our ride-sharing services on your travels?" + \
" (If so, 'yes' or hit any key for no.) ")
while select2 == "yes":
print("This adds a $20 additional cost per day.")
break
sum = ((select1 * rooms * nights) + (nights * rideshare))
print(format(sum))
【问题讨论】:
-
你在循环中放了什么?
while和break不在条件中与if相同。该循环只会运行一次。 -
sum = ((select1 * rooms * nights) + (nights * rideshare))你有str和int变量在这里混合在一起,所以这不会给你你正在寻找的值。考虑使用调试器单步执行代码并验证所有类型和值在每一行执行时是否符合预期。 -
select1和rideshare是字符串。将一个字符串乘以一个数字只会重复该字符串多次,即"hello" * 2得到hellohello。 -
查看这个可爱的debug 博客寻求帮助。显示变量值和类型的基本
print语句将突出显示问题。 -
所以 select1 的输入可以是 A B 或 C,它们都被声明为上面带有数字的常量。这是让我感到困惑的部分原因。然后rideshare也被声明为一个常数。我该怎么做才能修复它们?
标签: python variables while-loop sum constants