【问题标题】:ValueError: invalid literal for int() with base 10: python 3.9.1ValueError:以 10 为底的 int() 的无效文字:python 3.9.1
【发布时间】:2021-01-22 09:45:15
【问题描述】:

我正在为酒店预订系统创建一个代码,它会出现错误消息

ValueError: int() 以 10 为底的无效文字:

每次我运行它时都会出现此错误消息,但我尝试过的其他方法都没有效果

#this program is a hotel booking system
#this program was created on 18/01/21

def Display_heading_and_set_up_variables():
    global Hotel_Name, Price_Per_Night
    print("HOTEL BOOKING SYSTEM")
    Hotel_Name = str("Hotel_indigo")
    Price_Per_Night = float("69.99")

def Get_Number_Of_Nights():
    global No_Of_Nights, answer, confirmation
    #this part of the program will get the number of nights the person is staying for 
    answer = False
    while answer == False: 
            No_Of_Nights = input(int("How many nights are you going to stay for:"))
            while No_Of_Nights  <= 1  >= 21:
                print("That is an invalid entry - nights booked must be from 1 to 21")
                No_Of_Nights = input(int("How many nights are you going to stay for:"))
            confirmation = input(str("Have you chosen the correct number of nights you want to stay for:"))
            if confirmation == Y or confirmation == y:
                answer = True

def Discount_Calculator():
    global No_Of_Nights, Discount_percentage
    #this part of the program will work out the discount from the number of nights you stayed for
    if No_Of_Nights == 1  ==2:
        Discount_percentage = 0
    elif No_Of_Nights >= 3 <= 7:
        Discount_percentage = 5
    elif No_Of_Nights >= 8 <= 14:
        Discount_percentage = 10
    elif No_Of_Nights >= 15 <= 21:
        Discount_percentage = 15

def calculate_and_display_final_booking_details():
    #this part of the program will calculate and display your final information on booking
    global No_Of_Nights, Discount_percentage, Hotel_Name, Price_Per_Night, Discounted_Price_Per_Night
    Discounted_Price_Per_Night = Price_Per_Night / 100 * Discount_percentage + Price_Per_Night
    print("you have booked" ,No_Of_Nights, "number of nights")
    print("you qualify for a" ,Discount_Percentage, "% discount")
    print(" the final cost of your booking is:" ,Discounted_Price_Per_Night,)
#main program
Display_heading_and_set_up_variables()
Get_Number_Of_Nights()
calculate_and_display_final_booking_details()

我该如何解决这个问题?提前致谢

【问题讨论】:

  • 异常来自Python,而不是IDE,比如IDLE。

标签: python


【解决方案1】:

这是错误的

input(int("how many nights are you going to stay for:")

试试

int(input("how many nights are you going to stay for:"))

其他行也一样,你试图将字符串转换为int,这是错误的

【讨论】:

  • 感谢您的帮助,但现在说 y 和 Y 没有定义
  • 将 y 或 Y 放在引号 "" 中,这些都是简单的错误,开始谷歌搜索
  • 如果这是 OP 的问题,问题应该由于拼写错误而被关闭,而不是回答。请参阅How to Answer回答常见问题部分。
  • ...如果问题的定义如此不明确以至于没有只涉及一个特定问题,那么它应该被关闭,因为它太宽泛了。
猜你喜欢
  • 2016-10-17
  • 2018-09-09
  • 2020-01-04
  • 2010-12-22
  • 2011-07-07
  • 2019-10-14
相关资源
最近更新 更多