By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

I'm having trouble with a very simple while loop in 1 program, but not another.

P: 2
Hey everybody, this is my first post, so I hope I'm doing this right. I'm just starting to learn how to program, so I'm sorry if my error is obvious, but I can't seem to find the answer.

I created a very simple while loop in a test program that determines your dating range with a simple calculation, and then asks if you want to try again with a question, and will repeat until you decline.I t works perfectly. I tried doing the same thing in a very simple program that basically just calculates compound interest, but the while loop doesn't seem to work the same, despite being almost identical to my "dating age" program.

Here is the output/error message when I use the compound interest program:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Hello.
  3. Would you like to try? Please enter yes or no: yes
  4. How old are you? 19
  5. Please enter cash value: 10000
  6. Please enter interest rate in the form of a decimal: .1
  7. What age would you like to check your worth? 35
  8. Here is your worth: 45949.7298636
  9. Here is your profit: 35949.7298636
  10. Would you like to try again? yes
  11. Traceback (most recent call last):
  12.   File "/Users/johnhutchison/Documents/worth.py", line 4, in <module>
  13.     while decision in y:    
  14. TypeError: argument of type 'int' is not iterable
  15. logout
  16.  
  17. [Process completed]
  18.  
Here is the code to my "dating age" program, which works fine:

Expand|Select|Wrap|Line Numbers
  1. print "Welcome! Let's see who you can date!"
  2. y = [ 'yes', 'y', 'Yes', 'Y' ]
  3. decision = raw_input("Would you like to try? Please enter yes or no: ")
  4. while decision in y:
  5.     age = input("Please enter age: ")
  6.     age_min = age / 2 + 7 
  7.     age_max = (age - 7) * 2
  8.     print "Your dating range is between:", age_min, "and", age_max    
  9.     decision = raw_input("Would you like to try again? Please enter y or n: ")            
  10. raw_input("Thank you! Press Enter to exit.")
  11.  
Here is the code for my compound interest program, which won't seem to work:

Expand|Select|Wrap|Line Numbers
  1. print "Hello."
  2. y = [ 'yes', 'y', 'Yes', 'Y' ]
  3. decision = raw_input("Would you like to try? Please enter yes or no: ")
  4. while decision in y:    
  5.     age = input("How old are you? ")
  6.     p = input("Please enter cash value: ")
  7.     i = input("Please enter interest rate in the form of a decimal: ")
  8.     y = input("What age would you like to check your worth? ") - age
  9.     cash_worth = p * (1 + i)**y
  10.     profit = cash_worth - p
  11.     print "Here is your worth:", cash_worth
  12.     print "Here is your profit:", profit
  13.     decision = raw_input("Would you like to try again? ")
  14. raw_input("Press Enter to leave")
  15.  
I also realize that there are obvious other problems with my compound interest program, such as not rounding decimals, but I'm just using this to learn, so I'm tackling one thing at a time.

Thank you very much in advance for any help!
Nov 7 '10 #1

✓ answered by dwblas

The problem is that you are using single letter variable names and so an IDE or text processor can not find all instances of a variable. It can only find every time that letter was used any where. So
while decision in y:
TypeError: argument of type 'int' is not iterable
says that you set "y" to an integer value somewhere in the code, which is true, but I am going to leave it to you to find that particular "y" as a lesson in why we should use descriptive variable names.

Share this Question
Share on Google+
2 Replies


Expert 100+
P: 624
The problem is that you are using single letter variable names and so an IDE or text processor can not find all instances of a variable. It can only find every time that letter was used any where. So
while decision in y:
TypeError: argument of type 'int' is not iterable
says that you set "y" to an integer value somewhere in the code, which is true, but I am going to leave it to you to find that particular "y" as a lesson in why we should use descriptive variable names.
Nov 7 '10 #2

P: 2
Oh, I totally see my mistake! I assigned "y" twice. I feel like an idiot. I'll definitely be sure to start naming variables more descriptively. Thank you very much for your help and patience!
Nov 7 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.