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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • John Hutchison
    New Member
    • Nov 2010
    • 2

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

    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:

    Code:
     
    Hello.
    Would you like to try? Please enter yes or no: yes
    How old are you? 19
    Please enter cash value: 10000
    Please enter interest rate in the form of a decimal: .1
    What age would you like to check your worth? 35
    Here is your worth: 45949.7298636
    Here is your profit: 35949.7298636
    Would you like to try again? yes
    Traceback (most recent call last):
      File "/Users/johnhutchison/Documents/worth.py", line 4, in <module>
        while decision in y:	
    TypeError: argument of type 'int' is not iterable
    logout
    
    [Process completed]
    Here is the code to my "dating age" program, which works fine:

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

    Code:
    print "Hello."
    y = [ 'yes', 'y', 'Yes', 'Y' ]
    decision = raw_input("Would you like to try? Please enter yes or no: ")
    while decision in y:	
    	age = input("How old are you? ")
    	p = input("Please enter cash value: ")
    	i = input("Please enter interest rate in the form of a decimal: ")
    	y = input("What age would you like to check your worth? ") - age
    	cash_worth = p * (1 + i)**y
    	profit = cash_worth - p
    	print "Here is your worth:", cash_worth
    	print "Here is your profit:", profit
    	decision = raw_input("Would you like to try again? ")
    raw_input("Press Enter to leave")
    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!
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.

    Comment

    • John Hutchison
      New Member
      • Nov 2010
      • 2

      #3
      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!

      Comment

      Working...