problem with for loop

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

problem with for loop

Posted 21 February 2013 - 06:21 PM

Hi I'm new to Python. I am trying to get this program to run correctly but it says there is a problem with the for loop.

Here is the program:


# Purpose:
     # 	This program will convert 3 specified dollar amounts to Danish
     #	Krones, Euros, and Hong Kong Dollars. The user will enter the 
     #	dollar amounts and the exchange rates.

# Variable Declarations

us_dollars = 0
danish_krones ;
euros ;
hong_kong_dollars ;
number_of_us_dollars = 3 ;	
us_dollar_total = 0.0;
danish_exchange;
euros_exchange ;
hk_exchange ;
i ;
		
		
import sys;
		
print("Welcome to the Money Exchange\n\n") ;
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n\n") ;
print("First, please enter the exchange rates:\n\n");
print("Danish Krones: ");
danish_exchange = sys.argv;		# Reads the user input for Danish Krones

print("Euros: ");			# Reads the user input for Euros
euros_exchange = sys.argv;

print("Hong Kong Dollars: ") ;	# Reads the user input for Hong Kong Dollars
hk_exchange = sys.argv;

	# Begin for loop

for(i = 1; i <= number_of_us_dollars; ++i)
	
print("\n\n\nEnter US dollars i to be converted: ");		# Dollar amounts are entered by user */
us_dollars = sys.argv;

print("\n The amount of $us_dollars US dollars is equivalent to:\n\n");
		
danish_krones = us_dollars * danish_exchange ;		#US dollars are converted to other currencies */
euros = us_dollars * euros_exchange ;
hong_kong_dollars = us_dollars * hk_exchange ;
		
print("	danish_krones Danish Krones\n");			# Display dollar value in the other currencies */		
print("	euros Euros\n");
print("	$hong_kong_dollars Hong Kong Dollars\n\n");

us_dollar_total = us_dollar_total + us_dollars ;		# Accumulator for total US dollars converted */
		
		
	# End for loop
			
print("You have converted a total of $us_dollar_total dollars.\n" ); 
		


Is This A Good Question/Topic? 0
  • +

Replies To: problem with for loop

#2 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2017
  • View blog
  • Posts: 3,046
  • Joined: 21-June 11

Re: problem with for loop

Posted 22 February 2013 - 12:12 AM

The problem with your for loop is that the syntax you're using has nothing to do with the syntax of a for loop in Python.

I should also mention that your "variable declarations" section will cause a bunch of errors about you using undefined variables once you fix your syntax error. A variable exists once it has first been defined - just writing down its name does not "declare" a variable - it simply tries to access the variable.

You've been told before to learn the fundamentals of Python before trying to write programs in. You should do that.
Was This Post Helpful? 1
  • +
  • -

#3 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 22 February 2013 - 06:29 PM

Well I read how the syntax for the for loop should be but I don't see how I can use it to mimic the one I put in the program that is used in such languages as C and Java. Can you help me with this? Would I even use a for loop or something else?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: problem with for loop

Posted 22 February 2013 - 10:55 PM

Well your for loop in Python for:
for(i = 1; i <= number_of_us_dollars; ++i)
would probably be:
for i in range(1,number_of_us_dollars+1):
but this information will of course be completely useless to you.

For whatever unfathomable reason you seem to think that if you can't figure out how to write it in python, writing it in another language should work. It never will, ever. If you want to code in python, learn python.

-Mek
Was This Post Helpful? 1
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,149
  • Joined: 19-March 11

Re: problem with for loop

Posted 23 February 2013 - 12:24 AM

For whatever unfathomable reason you seem to think that if you can't figure out how to write it in python, writing it in another language should work. It never will, ever. If you want to code in python, learn python.


On a related note, that's a powerful lot of semicolons you've got there. Why?


To understand the for loop in python, think of "for each item in a set", and not, as in some other languages, "for each number in a range".

Think this:
for file in os.listdir("dir_name"):
    # do something to file



and not

files = os.listdir("dir_name")
for i in range (len (files)):
    # do something to files[i]

Was This Post Helpful? 0
  • +
  • -

#6 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 24 February 2013 - 06:33 PM

ok I think this is correct it seems to be fine. Is there a good Python IDE that is free you would recommend?

# Variable Declarations

us_dollars = 0
number_of_us_dollars = 3 	
us_dollar_total = 0.0;

		
		
import sys;
		
print("Welcome to the Money Exchange\n\n") 
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n\n") 
print("First, please enter the exchange rates:\n\n")
print("Danish Krones: ")
danish_exchange = sys.argv	# Reads the user input for Danish Krones

print("Euros: ")			# Reads the user input for Euros
euros_exchange = sys.argv

print("Hong Kong Dollars: ")  # Reads the user input for Hong Kong Dollars
hk_exchange = sys.argv

	# Begin for loop

for i in range(1,number_of_us_dollars+1):

	
     print("\n\n\nEnter US dollars i to be converted: ")		# Dollar amounts are entered by user */
     us_dollars = sys.argv

     print("\n The amount of us_dollars US dollars is equivalent to:\n\n")
		
     danish_krones = us_dollars * danish_exchange 	#US dollars are converted to other currencies */
     euros = us_dollars * euros_exchange 
     hong_kong_dollars = us_dollars * hk_exchange 
		
     print("	danish_krones Danish Krones\n")			# Display dollar value in the other currencies */		
     print("	euros Euros\n")
     print("	$hong_kong_dollars Hong Kong Dollars\n\n")

     us_dollar_total = us_dollar_total + us_dollars 		# Accumulator for total US dollars converted */
		
		
	# End for loop
			
print("You have converted a total of $us_dollar_total dollars.\n" )

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,149
  • Joined: 19-March 11

Re: problem with for loop

Posted 24 February 2013 - 07:10 PM

Quote

ok I think this is correct it seems to be fine.


Really? Did you test it? Does it actually do what you expect?

danish_exchange = sys.argv  # Reads the user input for Danish Krones



Um, no. It doesn't do that at all. In fact sys.argv doesn't do anything at all, it merely returns the list containing the command line arguments submitted by the used. See if you can use google to find out what command is used to get user input.


You need to learn the language, so an IDE would be conterproductive. Write your code in a text editor and use the python interpreter to test expressions. Don't use an IDE until you understand the syntax of the language.

Roll back to simpler programs and learn the basic concepts of the language. Learn to write them and debug them for yourself. Once you have got hold of those, then try to put them together into simple programs like this one. When you can write useful programs, and you're not struggling with syntax and basic concepts, then you can maybe think about an IDE - but for python there's really no need at all.

This post has been edited by jon.kiparsky: 24 February 2013 - 07:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 24 February 2013 - 08:55 PM

I read that this is correct:

danish_exchange = raw_input("Danish Krones: ")

Was This Post Helpful? 0
  • +
  • -

#9 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: problem with for loop

Posted 24 February 2013 - 09:40 PM

Gah. Just try it in the live interpreter. What does it do?
(Hint: if it doesn't work you are using python 3.)

Also you will have to do some more work to assure that your user is actually entering the type of data you want them to.

-Mek
Was This Post Helpful? 0
  • +
  • -

#10 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 26 February 2013 - 06:22 PM

Ok so I did this correctly I think but I'm still getting a few errors when running it. Here is the program:

us_dollars = 0
number_of_us_dollars = 3 	
us_dollar_total = 0.0;
i = 1
		
		
import sys;
		
print("Welcome to the Money Exchange\n\n") 
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n\n") 
print("First, please enter the exchange rates:\n\n")

danish_exchange = raw_input("Danish Krones: ")	# Reads the user input for Danish Krones
			
			# Reads the user input for Euros
euros_exchange = raw_input("Euros: ")

  # Reads the user input for Hong Kong Dollars
hk_exchange = raw_input("Hong Kong Dollars: ")

	# Begin for loop

for i in range(1,number_of_us_dollars+1):

	
     		# Dollar amounts are entered by user */
     us_dollars = raw_input("\n\n\nEnter US dollars %i to be converted: " % (int(i))

     print "\n The amount of %i US dollars is equivalent to:\n\n" % (int(us_dollars))
		
     danish_krones = us_dollars * danish_exchange 	#US dollars are converted to other currencies */
     euros = us_dollars * euros_exchange 
     hong_kong_dollars = us_dollars * hk_exchange 
		
     print "	%i Danish Krones\n" (int(danish_krones))			# Display dollar value in the other currencies */		
     print "	%i Euros\n" (int(euros))
     print "	%i Hong Kong Dollars\n\n" (int(hong_kong_dollars))

     us_dollar_total = us_dollar_total + us_dollars 		# Accumulator for total US dollars converted */
		
		

			
print "You have converted a total of %i dollars.\n" (int(us_dollar_total))


It keeps saying invalid syntax at this line:

 print "\n The amount of %i US dollars is equivalent to:\n\n" % (int(us_dollars))

Was This Post Helpful? 0
  • +
  • -

#11 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: problem with for loop

Posted 27 February 2013 - 01:01 AM

If you have a syntax error that means your program won't run... so... why would you think the rest of it was correct?

The simple answer is you missed a parenthesis, not on the line that the error is showing, but on the previous line.

So change that line to:
us_dollars = raw_input("\n\n\nEnter US dollars %i to be converted: " % (int(i)))
This will at least fix the syntax error. You will of course immediately hit another error as you never converted any of your inputs to numbers and are trying to do math with strings. Once you fix all of that and your program has no actual glitches; then tell me if it does what you expect.

-Mek
Was This Post Helpful? 1
  • +
  • -

#12 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 27 February 2013 - 03:48 AM

Ok I think I'm good now, it seems to run fine. Here it is:

us_dollars = 0
number_of_us_dollars = 3 	
us_dollar_total = 0.0;
i = 1
		
		
import sys;
		
print("Welcome to the Money Exchange\n\n") 
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n\n") 
print("First, please enter the exchange rates:\n\n")

danish_exchange = float(input("Danish Krones: "))	# Reads the user input for Danish Krones
			
			# Reads the user input for Euros
euros_exchange = float(input("Euros: "))

  # Reads the user input for Hong Kong Dollars
hk_exchange = float(input("Hong Kong Dollars: "))

	# Begin for loop

for i in range(1,number_of_us_dollars+1):

	
     		# Dollar amounts are entered by user */
     us_dollars = float(input("\n\n\nEnter US dollars %i to be converted: " % (int(i))))

     print "\n The amount of %.2f US dollars is equivalent to:\n\n" % (float(us_dollars))
		
     danish_krones = us_dollars * danish_exchange 	#US dollars are converted to other currencies */
     euros = us_dollars * euros_exchange 
     hong_kong_dollars = us_dollars * hk_exchange 
		
     print "	%.2f Danish Krones\n" % (float(danish_krones))			# Display dollar value in the other currencies */		
     print "	%.2f Euros\n" % (float(euros))
     print "	%.2f Hong Kong Dollars\n\n" % (float(hong_kong_dollars))

     us_dollar_total = us_dollar_total + us_dollars 		# Accumulator for total US dollars converted */
		
		

			
print "You have converted a total of %.2f dollars.\n" % (float(us_dollar_total))

Was This Post Helpful? 0
  • +
  • -

#13 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: problem with for loop

Posted 27 February 2013 - 05:58 AM

Ok. This is starting to resemble python so I will try to make some constructive suggestions. In python 2.x (as evidenced by your print statements) you need to use raw_input, not input; raw_input will return a string (which is what you want); input attempts to evaluate the input as code (which is bad-bad). Next as you seem to be realizing, semicolons are unnecessary at the end of lines in python; the same goes for */ commenting. Finally, now that you have a working program you need to try to anticipate bad user inputs. What if a user doesn't enter a number? As your program stands, if any user enters a non-numeral for any of the inputs, the program will crash.

One last problem is purely aesthetic, without actually rewriting any of your code, it would appear more readable as follows:
print("Welcome to the Money Exchange\n\n")
print("The program will convert three US dollar amounts to Danish, Krones, Euros and Hong Kong Dollars.\n\n")
print("First, please enter the exchange rates:\n\n")

danish_exchange = float(raw_input("Danish Krones: "))	# Reads the user input for Danish Krones
euros_exchange = float(raw_input("Euros: ")) # Reads the user input for Euros
hk_exchange = float(raw_input("Hong Kong Dollars: ")) # Reads the user input for Hong Kong Dollars

# Begin for loop
number_of_us_dollars = 3
us_dollar_total = 0
for i in range(1,number_of_us_dollars+1):
    # Dollar amounts are entered by user
    us_dollars = float(raw_input("\n\n\nEnter US dollars %i to be converted: " % i))
    print "\n The amount of %.2f US dollars is equivalent to:\n\n" % us_dollars
    #US dollars are converted to other currencies
    danish_krones = us_dollars * danish_exchange 	
    euros = us_dollars * euros_exchange
    hong_kong_dollars = us_dollars * hk_exchange
    # Display dollar value in the other currencies
    print "	%.2f Danish Krones\n" % danish_krones			
    print "	%.2f Euros\n" % euros
    print "	%.2f Hong Kong Dollars\n\n" % hong_kong_dollars
    # Accumulator for total US dollars converted
    us_dollar_total += us_dollars

print "You have converted a total of %.2f dollars.\n" % us_dollar_total

Once you get more of a grip on the syntax and basics of python you should attempt to rewrite this as a function. This is a very simple program but in general global variables are not a good thing. Code shouldn't just all be spewed into the global namespace.

I hope you continue to program in Python and good luck to you,
-Mek
Was This Post Helpful? 1
  • +
  • -

#14 ferguson32  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 178
  • Joined: 29-May 12

Re: problem with for loop

Posted 27 February 2013 - 05:10 PM

Thanks for the tips. I'm going to clean up the program after it's complete. Im trying to do as you said and control the user input and I'm havin a tough time with the loop. This is how I thought it should be:

danish_exchange = float(raw_input("Danish Krones: "))	# Reads the user input for Danish Krones
while True:
     if danish_exchange <= 0:
	  print"Invalid entry"
	  danish_exchange = True
     elif danish_exchange > 0:
	  continue

This post has been edited by ferguson32: 27 February 2013 - 05:10 PM

Was This Post Helpful? 0
  • +
  • -

#15 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: problem with for loop

Posted 27 February 2013 - 05:38 PM

This is a fairly standard method:
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.")

float will automatically raise a ValueError if passed a string it can't convert, so we catch the exception, give the user a warning and loop. If the user enters a number that isn't greater than zero, we explicitly raise a ValueError. If the user follows the rules, we break out of the loop.

Cheers,
-Mek
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2