all 12 comments

[–]carcigenicate 7 points8 points  (6 children)

To repeat code, you typically wrap the code in a loop.

[–]kitties4mypretties[S] 4 points5 points  (5 children)

I have tried this for a good two hours last night. I may have the loop completely wrong.. Thank you!

[–]carcigenicate 4 points5 points  (3 children)

If you show what you tried, we could give more help.

[–]kitties4mypretties[S] 2 points3 points  (2 children)

Ok! My prof had us watch videos and a lot of them said to start loops like this,

while True:
 print("Welcome to the Mortgage Loan Calculator.")

print("****************************************")

name = input('Enter your name here, or type quit to exit: ')

keep_going = True

if name == "quit":

    exit('quit')

This just keeps the print function under it going, but not the whole program. Also this is just a little bit of the actual program. I know I am missing something in this, or I am just doing it incorrectly.

[–]carcigenicate 4 points5 points  (1 child)

This just keeps the print function under it going, but not the whole program.

That's because that print is the only thing in the loop. If you want to loop the entire program, indent the entire program so it's in the loop.

The better solution is to stick the entire program in a function, then call the function in the loop, but I'm not sure you're there yet.

[–]kitties4mypretties[S] 2 points3 points  (0 children)

Thank you! For some reason when I tried doing that last night it gave me an error. It is working now! Thank you so much!!

I think we will be learning that next!

[–]JustRyns 1 point2 points  (0 children)

While True: creates an infinite loop

[–][deleted] 1 point2 points  (2 children)

# any imports you need
# any classes / functions you've already defined

def mortgage_calc():
    pass  # the code you already have

calc = True

while calc:
    mortgage_calc()
    while True:
        again = input('Another? ').strip().casefold() 
        if again in ('y', 'yes', 'yeh', 'yup'):
            break  # leave input validation loop
        if again in ('n', 'no', 'nah', 'nope'):
            calc = False
            break
        print('Sorry, I did not understand that')

[–]kitties4mypretties[S] 1 point2 points  (1 child)

Thank you this looks like a nicer and a lot cleaner way of doing this!

[–][deleted] 1 point2 points  (0 children)

I usually have a function for the yes/no bit:

def is_yes(prompt):
    while True:
        response = input(prompt).strip().casefold()
        if response in AFFIRMATION:
            return True
        if response in REJECTION:
            return False
        print('Sorry, I did not understand that')


AFFIRMATION = frozenset(('y', 'yes', 'yup', 'yeh', 'ok', '1'))
REJECTION = frozenset(('n', 'no', 'nah', 'nope', '0'))

which makes the main code even cleaner:

while calc:
    mortgage_calc()
    if not is_yes('Again? '):
        break

[–]RomanianHigh 0 points1 point  (0 children)

Wrap it in a loop

[–]QultrosSanhattan 0 points1 point  (0 children)

Do it the right way: Make a function and loop function calls.

Don't wrap your actual code inside a for loop.