简体   繁体   English

未定义变量 — Python — 条件语句

[英]Variable not defined — Python — Conditional Statements

So I'm having issues with this code.所以我遇到了这段代码的问题。 When I run it it states that the variable cost_of_rental is not defined.当我运行它时,它指出变量 cost_of_rental 没有定义。 What is it I'm doing wrong?我做错了什么?

Here is my code:这是我的代码:

type_car = input("Welcome to CarRental. What type of car would you like to rent?  ")

rent_duration = int(input("For how many days do you wish to rent this car?  "))

available_classes = ['Class B', 'Class C', 'Class D']

if type_car == 'Class B':
    if rent_duration <= 6:
        cost_of_rental = 27 * rent_duration

    elif rent_duration <= 27:
        cost_of_rental = 167 + (rent_duration-7)*25

    elif rent_duration <= 60:
        cost_of_rental = 662 + (rent_duration-28)*23

if type_car == 'Class C': 
    if rent_duration <= 6: 
        cost_of_rental = 34*rent_duration

    elif rent_duration <= 27:  
        cost_of_rental = 204 + (rent_duration - 7)*31

    elif rent_duration <= 60:
        cost_of_rental = 810 + (rent_duration - 28)*28

if type_car == 'Class D':
    if rent_duration <= 6: 
        print("Sorry, Class D cars cannot be rented for less than 7 days.")

    elif rent_duration <= 27: 
        cost_of_rental = 810 + (rent_duration-28)*43

    elif rent_duration <= 60:
        cost_of_rental = 1136 + (rent_duration - 28)*38


print("Your total cost is:  ", cost_of_rental)

As comments to your question suggest you should declare cost_of_rental variable beside you conditions.正如对您问题的评论建议您应该在条件旁边声明cost_of_rental变量。 Currently if none of your condition is True than your cost_of_rental variable is not defined.目前,如果您的条件都不为真,则您的cost_of_rental变量未定义。 In another words - you define it only if you satisfy any of your conditions.换句话说 - 只有当您满足任何条件时,您才能定义它。 Adding cost_of_rental = 0 on the beginning or else (not elif ) statements to your conditions should solve your issue.在您的条件中添加cost_of_rental = 0else (不是elif )语句应该可以解决您的问题。

Since your if / elif structure has no else block, there are paths through your code which do not assign a value to the variable cost_of_rental .由于您的if / elif结构没有else块,因此您的代码中有一些路径不会为变量cost_of_rental赋值。 If these paths should never be taken by a valid rent_duration , I would recommend dealing with it by raising a ValueError to indicate that rent_duration is invalid.如果这些路径不应该被有效的rent_duration ,我建议通过引发ValueError来指示rent_duration无效来处理它。

Note that the same applies to your outer if statements - if type_car isn't one of the options you've checked for, I assume this means the input is invalid, so raising a ValueError is appropriate.请注意,这同样适用于您的外部if语句 - 如果type_car不是您检查过的选项之一,我认为这意味着输入无效,因此引发ValueError是合适的。 Raising an error means your code won't continue as normal, so the print statement at the bottom won't be reached (which is what you want when the input is invalid).引发错误意味着您的代码将无法正常运行,因此将无法到达底部的 print 语句(这是您在输入无效时想要的)。

If you want to print a friendly error message to the user instead, use try / catch .如果您想向用户打印友好的错误消息,请使用try / catch Generally, it is not a good idea to just print the error message, because your code will continue executing with the invalid data, which usually means you'll get a result that doesn't make sense (eg printing the error message and then still printing a total cost afterwards).通常,只print错误消息不是一个好主意,因为您的代码将继续使用无效数据执行,这通常意味着您会得到一个没有意义的结果(例如打印错误消息然后仍然之后打印总成本)。

type_car = input("Welcome to CarRental. What type of car would you like to rent?  ")

rent_duration = int(input("For how many days do you wish to rent this car?  "))

available_classes = ['Class B', 'Class C', 'Class D']

if type_car == 'Class B':
    if rent_duration <= 6:
        cost_of_rental = 27 * rent_duration

    elif rent_duration <= 27:
        cost_of_rental = 167 + (rent_duration-7)*25

    elif rent_duration <= 60:
        cost_of_rental = 662 + (rent_duration-28)*23

    else:
        raise ValueError('Invalid rent duration')

elif type_car == 'Class C': 
    if rent_duration <= 6: 
        cost_of_rental = 34*rent_duration

    elif rent_duration <= 27:  
        cost_of_rental = 204 + (rent_duration - 7)*31

    elif rent_duration <= 60:
        cost_of_rental = 810 + (rent_duration - 28)*28

    else:
        raise ValueError('Invalid rent duration')


elif type_car == 'Class D':
    if rent_duration <= 6: 
        raise ValueError("Sorry, Class D cars cannot be rented for less than 7 days.")

    elif rent_duration <= 27: 
        cost_of_rental = 810 + (rent_duration-28)*43

    elif rent_duration <= 60:
        cost_of_rental = 1136 + (rent_duration - 28)*38

    else:
        raise ValueError('Invalid rent duration')

else:
    raise ValueError('Invalid car type')


print("Your total cost is:  ", cost_of_rental)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM