Dec-16-2023, 03:53 PM
I'm playing around with globals and managing variables, and I want to be able to check if a variable exists without the program stopping. If the variable exists, something happens, if the variable does not exist, something else happens.
Is this the right approach to use for that sort of task? Or should I be using other functions or techniques?
Is this the right approach to use for that sort of task? Or should I be using other functions or techniques?
car_name = "Tesla Model S" car_year = 2022 def delete_car_variables(): global car_name, car_year del car_name del car_year print("Before deletion:") print("Car Name:", car_name) print("Car Year:", car_year) delete_car_variables() print("\n") print("After deletion:") try: print("Car Name:", car_name) except: print("Car Name is deleted") try: print("Car Year:", car_year) except: print("Car Year is deleted")