How to make global list inside function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to make global list inside function (/thread-31151.html) |
How to make global list inside function - CHANKC - Nov-25-2020 from output import welcome, more, byebye, summary, invalid, emptylist, close def food(): global choice x = 1 while x > 0 and x <= 15: try: more() x = int(input("Enter your choice: ")) choice.append(x) except: invalid() else: choice.remove(20) if len(choice) == 0: emptylist() else: choice.sort() print ("This is your selection:",choice) close() print(choice)the code is shown above but I can't do "print(choice)" RE: How to make global list inside function - Larz60+ - Nov-25-2020 you need to indent lines 1 and 19 you should avoid using globals, instead, return choice. example: def food(): x = 1 while x > 0 and x <= 15: try: more() x = int(input("Enter your choice: ")) choice.append(x) except: invalid() else: choice.remove(20) if len(choice) == 0: emptylist() else: choice.sort() print ("This is your selection:",choice) close() return choice print(food()) RE: How to make global list inside function - CHANKC - Nov-25-2020 Alright. Thank you so much but the actual problem is as below: #main Module from output import menu from userinput import userinfo, orders, food menu() userinfo() orders() print (choice) #userinput Module from output import welcome, more, byebye, summary, invalid, emptylist, close def userinfo(): x = input("Enter your name: ").title() y = input("Enter your phone number: ") a = welcome(x) choice = [] def orders(): order = int(input("Enter your choice: ")) while order >= 0: try: if order == 0: byebye() order = -1 elif order >= 1 and order <= 15: choice.append(order) food() order = -1 elif order == 16: summary() order = -1 elif order >= 17: emptylist() order = int(input("Enter your choice: ")) except: invalid() def food(): x = 1 while x > 0 and x <= 15: if x > 0 and x <= 15: more() x = int(input("Anymore? ")) choice.append(x) else: invalid() else: choice.remove(20) if len(choice) == 0: emptylist() else: choice.sort() close() print ("This is your selection:",choice) #output Module #Order Menu def menu(): print ( ''' Welcome to Sonic Kitchen Takeaway ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~Please select your choice~~ | | -----Meat--RM3.00/dish -----Rice----- | | [1] Butter Chicken [11] Half Rice (RM1.00) | | [2] Sweet and Sour Chicken [12] Full Rice (RM2.00) | | [3] Steam Fish | | [4] Butter Pork -----Soup--RM4.00/dish | | [5]Gilled Pork [13] Chicken Soup | | [14] Fish Soup | | -----Vegetable--RM2.00/dish [15] Vegetable Soup | | [6] Fried Cabbage | | [7] Fried Kale [16] Order Summary | | [8] Fried Spinach | | [9] Tomato Egg | | [10]Fried Bean Sprout [0] Exit | ---------------------------------------------------------------------- Operating Hour: 10am - 7pm Contact Number: 019 - 1385 3375 ''' ) return ("") def welcome(x): print("Hi,",x,'.Welcome to Sonic Kitchen delivery.') def more(): print("Thank you! Enter 20 to exit from order.") def byebye(): print("Thank you for visiting Sonic Kitchen Denivery!") def summary(): print("View Summary") def invalid(): print("Invalid input. Try Again.") def emptylist(): print("You did not order anything.") def close(): print("Thank you for ordering Sonic Kitchen!")So with the three modules. I run the MAIN module but the error state is as below: NameError: name 'choice' is not defined RE: How to make global list inside function - palladium - Nov-25-2020 Line 10, try: print(userinput.choice) RE: How to make global list inside function - CHANKC - Nov-25-2020 Nope. It is still error #Main Module from output import menu from userinput import userinfo, orders, food menu() userinfo() orders() print(userinput.choice)
RE: How to make global list inside function - perfringo - Nov-25-2020 I know nothing about the code above but indeed in this snippet there is no userinput defined. from userinput import userinfo, orders, foodMaybe you should import from userinput also choice? RE: How to make global list inside function - CHANKC - Nov-26-2020 It is solved with import choice from userinput. Thank you so much. |