简体   繁体   中英

Conditional doesn't make sense

I have a error handle module, here is one that error handle a license plate:

def is_plate(prompt=""):
    format_plate = re.compile(r'[A-Za-z]{3}\d{3}$')
    while True:
        p = input(prompt).upper()
        if format_plate.match(p):
            return p
        else:
            print("Invalid license plate, try again. Your license plate needs to be in this format:(abc123)")

I imported that module to implement it in one of my method in my program, Here is a snippet of my program:

import error_handle as eh
class Car:
    def __init__(self,name,car_type,reg_nr):
        self.owner = name
        self.car_type = car_type
        self.reg_nr = reg_nr

    def __str__(self):
        return f'{self.owner},{self.car_type},{self.reg_nr}'
class Garage:
    def __init__(self):
        self.parking_log_list = []
        self.account_list = []

def changing_info(self,license_plate):
    for car in self.account_list:
        if license_plate == car.reg_nr:
            edit_choice = int(input("Choose what you like to change:\n1.License Plate\n2.Name\n3.Car Type\n"))
            if edit_choice == "1":
                car.reg_nr = eh.is_plate("State your new license plate: ")
            if edit_choice == "2":
                car.owner = input("State your Full name: ")
            if edit_choice == "3":
                car.type = eh.is_alpha("State your new car type: ")
garage = Garage()
license_plate = eh.is_plate("State your license plate: ")
garage.changing_info(license_plate)

When I run the program this is the following output:

State your license plate: abc456
Process finished with exit code 0

It doesn't execute the next line of code

When I change the conditional statement to this:

if license_plate != car.reg_nr:

It continues to execute the next line

I don't understand why it works with,= operator? shouldn't it be the == operator instead,? or is it something wrong with my function on my error handle module?

It's running when you have the != operator because at your for loop you are looping through self.account_list although when it is looping, this list is empty. So it is setting car to None . Because of this it isn't executing that code with the == operator because the license plate isn't equal to None .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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