How to use a global variable for a class object creation?-Python
Python:::Hei everyone . My problem is that i cant use a global variable outside of an other function. I need it though in my example. Here is the code peace im struggeling with #================================================== supermarketname = "test" def supermarket_check_name(): global supermarketname check = True while check: supermarketname_to_check = input("Enter a Supermarket name: ") if len(supermarketname_to_check) >19: print("This name is to long") else: supermarketname = supermarketname_to_check check = False class supermarket_class(): def __init__(self,name,bananas,apples,melones,peaches,coconut): self.name = name self.bananas = bananas self.apples = apples self.melones = melones self.peaches = peaches self.coconut = coconut supermarket = supermarket_class(supermarketname,1,2,3,4,5) print(supermarket.name) #================================================ #=====================Output===================== #when the input pops up and u type in Bobssupermarket it still prints test #=============================================== when i would add a function like : def print_supermarketname(): print(supermarketname) and call it after the supermarket_check_name function it gives me the input as name, but i cant put it in a class. Hope someone can help me. Thannks. z77