[Tutor] Expected indented block error

Alan Gauld alan.gauld at btinternet.com
Mon Sep 23 19:14:48 CEST 2013


On 21/09/13 23:44, Arron Sutcliffe wrote:
> Hi i have been working on a currency converter for school and i have
> been having a hard time with this syntax error "Expected indented block"
> and i don't understand why it isn't working.

It tells you, it's expecting an indented block...

> v_fallback = 1
> while v_fallback == 1:
>      print("Please enter your current currency")
>      v_current = input()
>      if v_current == ("USD" or "usd" or "US Dollars" or "us dollars"):
>          print("Please enter the name of the currency you wish to
> convert to")
>          print("Your options are GBP, EUR, INR, AUD, CAD, AED or BACK to
> return to the Main Menu")
>          v_final = input()
>          if v_final ==("GBP" or "gpb"):

Python is expecting an indented block of code to do something when the 
if is true. So eoitrher you put some code here or...

>          if v_final == ("EUR" or "eur"):

You indent this line. But thats probably a bad idea.

>          if v_final == ("INR" or "inr"):
>          if v_final == ("AUD" or "aud"):
>          if v_final == ("CAD" or "cad"):

BTW these lines are almost certainly not doing what you think they are.
This is not testing whether v_final is one of the values in the if tet, 
it is testing whether v_final has the same truth(boolewan0 vale as the 
expression (str1 or str2) which is always True.

You probably want something like

if v_final.upper() == "GBP": # do something

or, more likely in this case:

if v_final.upper() in ("GBP", "INR", "AUD", ...other values): #do this

>          if v_final == ("AED" or "aed"):
>          if v_final == ("BACK" or "back"):
>          else:
>              print ("ERROR - Please enter a valid currency, if incorrect
> currency is entered again program will close")
>              print("Your options are GBP, EUR, INR, AUD, CAD, AED or
> BACK to return to the main menu")
>              v_final = input()
>              if v_final == ("GBP" or "gbp"):
>              if v_final == ("EUR" or "eur"):
>              if v_final == ("INR" or "inr"):
>              if v_final == ("AUD" or "aud"):
>              if v_final ==("CAD" or "cad"):
>              if v_final == ("AED" or "aed"):
>              if v_final == ("BACK" or "back"):
>          else print ("ERROR - Program will close"):
>              v_fallback = 0


And there is a programming principle called DRY (Don't Repeat Yourself).
You have the same set of tests here as above. Rather than limit the user 
to 2 goes you could wrap the tests into a while loop and only have them 
once. That way there is never any risk of them getting out of step
with each other. (You could also encapsulate them in a function but you 
might not have covered functions yet!)


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list