简体   繁体   中英

I keep getting "expected an indented block" when running my python code on idle

This is it.

while userInputValid != True: 
            try:
print("Welcome to Domino's Pizza Uganda! SUPA PIZZA!!! DIRECT FROM WAKALIWOOD ZULUL Where SUPA-PACKED movies are made!")
region = input("What is your region? \n 1.) Central\n 2.) Western \n 3.) Eastern\n 4.) Northern\n\n")

print ("Region:", region, "selected!\n\n")


if region == 1 or region == 2 or region == 3 or region == 4:
      userInputValid = True

else:
      print("Unexpected number! Please choose numbers 1, 2, 3 or 4.")

except ValueError:
      print("Numbers only please!\n Single digits like 1, 2, 3 or 4.")

      if region == 1:
            print("Central region selected! \n\n")
             if region == 2:
                   print("Western region selected! \n\n")
                   if region == 3:
                         print("Eastern region selected! \n\n")
                         if region == 4:
                               print("Northern region selected! \n\n")

pickupdelivery = input("Pick-up or SUPA Delivery?!\n \n 1 = Pick-up. \n 2 = SUPA DELIVERY!")



print(pickupdelivery, "picked!")

I have no idea whats wrong with it

White space defined structure in python which is what makes it, generally, more readable than many other languages. However that makes it very important that you pay attention to the indentation. Get one line at the wrong indent level can either cause errors, or worse act differently than you expected.

I would also encourage you to run flake8 ( http://flake8.pycqa.org/en/latest/ ) on your code which will pick up a lot of mistakes and formatting errors before you get to the code-running stage.

I believe that you want it to look more like this:

while userInputValid != True:
    try:
        print("Welcome to Domino's Pizza Uganda! SUPA PIZZA!!! DIRECT FROM WAKALIWOOD ZULUL Where SUPA-PACKED movies are made!")
        region = input("What is your region? \n 1.) Central\n 2.) Western \n 3.) Eastern\n 4.) Northern\n\n")

        print ("Region:", region, "selected!\n\n")

        if region == 1 or region == 2 or region == 3 or region == 4:
              userInputValid = True
        else:
              print("Unexpected number! Please choose numbers 1, 2, 3 or 4.")
    except ValueError:
        print("Numbers only please!\n Single digits like 1, 2, 3 or 4.")

    if region == 1:
        print("Central region selected! \n\n")
    if region == 2:
        print("Western region selected! \n\n")
    if region == 3:
        print("Eastern region selected! \n\n")
    if region == 4:  
        print("Northern region selected! \n\n")
    pickupdelivery = input("Pick-up or SUPA Delivery?!\n \n 1 = Pick-up. \n 2 = SUPA DELIVERY!")
    print(pickupdelivery, "picked!")

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