problem with for loop

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 2616 Views - Last Post: 27 February 2013 - 06:59 PM Rate Topic: -----

#16 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: problem with for loop

Posted 27 February 2013 - 06:07 PM

I tried that code that you wrote but it keeps giving me an error where it says break.

while 1:
     try:
	  danish_exchange = float(raw_input("Danish Krones: "))	# Reads the user input for Danish Krones
	  if danish_exchange <= 0:
	     raise ValueError
	break
     except ValueError:
	  print("Invalid entry. Please enter a number > 0.")

Was This Post Helpful? 0
  • +
  • -

#17 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 117
  • View blog
  • Posts: 215
  • Joined: 11-January 13

Re: problem with for loop

Posted 27 February 2013 - 06:51 PM

Indentation is critical in python.

This:
while 1:
     try:
	  danish_exchange = float(raw_input("Danish Krones: "))	# Reads the user input for Danish Krones
	  if danish_exchange <= 0:
	     raise ValueError
	break
     except ValueError:
	  print("Invalid entry. Please enter a number > 0.")


Is not the same as this:
while 1:
    try:
        danish_exchange = float(raw_input("Danish Krones: "))
        if danish_exchange <= 0:
            raise ValueError
        break
    except ValueError:
        print("Invalid entry. Please enter a number > 0.")



Also proper indentation should really be 4-spaces. More than 4 is fine if you feel the need, but any less starts getting hard to read. Above all, be consistent.
-Mek
Was This Post Helpful? 1
  • +
  • -

#18 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 191
  • Joined: 29-May 12

Re: problem with for loop

Posted 27 February 2013 - 06:59 PM

Thanks, I was trying to use tab but it wasn't working so I tried the space bar and it indented, weird.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2