我不知道如何添加这些元素,sum/while循环困难吗?

2024-10-02 16:25:31 发布

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

基本上,我试图得到一个粗略的总数在这里根据给定的输入。我不知道为什么我不能让它加上,尽管使用总和。我试着把整个事情放到一个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))

Tags: theyouforinputyourdiscountbewill
3条回答

select1rideshare是字符串,而不是数字。select1包含用户输入的酒店名称,但您需要相应酒店的价格。您可以使用字典存储每个酒店的数字价格:

hotels = {"HOLIDAY": 180,
          "SHOWB": 230,
          "MOLLYP": 180}

下面是如何从用户输入中获取速率:

while select1 not in hotels:
    # Ask which hotel
    select1 = input("At which hotel will you be staying? " + \
                     "(Enter the capital letter that corresponds.)")

rate = hotels[select1] # get rate of hotel from dictionary

对于rideshare,需要根据用户输入将其设置为0或20:

# 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.) ")
if select2 == "yes":
    print("This adds a $20 additional cost per day.")
    rideshare = rideshare_rate
else: rideshare = 0

下面是一个有效的实现:

rideshare_rate = 20

# Declare variables
rooms = 0
hotel = 0
select1 = ""

hotels = {"HOLIDAY": 180,
          "SHOWB": 230,
          "MOLLYP": 180}

# 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")

while select1 not in hotels:
    # Ask which hotel
    select1 = input("At which hotel will you be staying? " + \
                     "(Enter the capital letter that corresponds.)")

rate = hotels[select1] # get rate of hotel from dictionary

# 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.) ")
if select2 == "yes":
    print("This adds a $20 additional cost per day.")
    rideshare = rideshare_rate
else: rideshare = 0

sum = (rooms * nights) + (nights * rideshare)
print(format(sum))

在rideshare中,您声明字符串“yes” 在MOLLYP中声明字符串“A”

holidayInn = 120
showBoat = 230
mollyPitcher = 180
rideshare = 20
A = 180
B = 230
C = 120

# Declare variables
rooms = 0
hotel = 0
rideshare = "yes" # because of that you have yes in your result
MOLLYP = "A" # declare string A in variable MOLLYP (A in your result) 
SHOWB = "B"
HOLIDAY = "C"

可能你想声明如下:

rideshare = 20 # 20
MOLLYP = A  # 180
SHOWB = B  # 230
HOLIDAY = C #120

so input to select1 can be either A B or C, which are all declared as constants above with numbers. That's part of what's confusing me.

select1是值为"A""B""C"的字符串。还有名为ABC的变量。这两件事之间没有联系。select1不会神奇地接受相应字母变量的值。你知道吗

如果您想这样做,请使用字典:

hotels = {
    "A": 120,  # Holiday Inn
    "B": 230,  # Showboat
    "C": 180   # Molly Pitcher Inn
}

choice = input("Enter hotel letter: ")
if choice in hotels:
    hotel_cost = hotels["choice"]
else:
    print("I don't recognize that hotel")

或者,因为只有三种选择,所以它可能同样容易使用if/else:

choice = input("Enter hotel letter: ")
if choice == "A":
    hotel_cost = 120
elif choice == "B":
    hotel_cost = 230
elif choice == "C":
    hotel_cost = 180
else:
    print("I don't recognize that hotel")

相关问题 更多 >