-1

im new to coding and i wanted to see if there is anyway to simplify the if/else statements cause even for a new coder, this looks disgusting.

Any help would be appreciated :D

def budget():
    
    global budg

    reply = "0"
    while reply != "1" and reply != "2" and reply != "3" and reply != "4" and reply != "5" and reply != "6":
        print("                                           ")
        print("         YOU HAVE SELECTED", local,"       ")
        print("          PLEASE ENTER YOUR BUDGET         ")
        print("                                           ")
        print("         1.  UNDER £10                     ")
        print("         2.  £10 - £20                     ")
        print("         3.  £20 - £30                     ")
        print("         4.  £30 - £40                     ")
        print("         5.  £40 - £50                     ")
        print("         6.  £50 ABOVE                     ")
        print("                                           ")
        
        #DEPICS WHAT LOCATION THE USER REQUESTS
        reply = input("Please enter a valid option (1-6)\n")
        if reply == '1':
            local = 'UNDER £10'
        elif reply == '2':
            local = '£10 - £20'
        elif reply == '3':
            local = '£20 - £30'
        elif reply == '4':
            local = '£30 - £40'
        elif reply == '5':
            local = '£40 - £50'
        elif reply == '6':
            local = '£50 ABOVE'
        else:
            print("Please enter a valid option (1-4) \n")

1 Answers1

4

Use a dictionary to store your alternatives:

try:
   local = {
      '1': 'UNDER £10',
      '2': '£10 - £20',
      '3': '£20 - £30',
      '4': '£30 - £40',
      '5': '£40 - £50',
      '6': '£50 ABOVE'
   }[reply]
except KeyError:
   print("Please enter a valid option (1-4) \n")
qouify
  • 3,698
  • 2
  • 15
  • 26