Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make my program return to the beginning in Python?

Tags:

python

I'm a really new programmer, just programing for fun. I usually write my programs with a series of while loops to get to different sections of the program at different times. It's probably not the best way, but it's all I know how to do.

For example, I'm working on a little program that runs in a shell meant to solve an equation. Here's part of the program that's supposed to bring you back to the beginning.

while loop==4:
    goagain = raw_input("Would you like to do the equation again? (Y/N)")
    if goagain == "Y":
        #Get values again
        loop=2
    elif goagain == "N":
        print "Bye!"
        #End program
        loop=0
    else:
        print"Sorry, that wasn't Y or N. Try again."

I have it set so that while loop==2, it gets the values to put into the equation and while loop==0, nothing happens.

The problem I have, though, is that when my loop changes back to 2, the program just ends there. It doesn't want to back in the code to where I told it what to do while loop==2.

So what I need to know is how to get my program to go back to that section. Should I use a different method than the while loops? Or is there a way to get my program to go back to that section?

Thanks!

like image 805
user1247096 Avatar asked May 03 '26 10:05

user1247096


1 Answers

A better approach would be to do sometihng like this:

while True:
    goagain = raw_input("Would you like to do the equation again? (Y/N)")
    if goagain == "Y":
        #Get values again
        pass #or do whatever
    elif goagain == "N":
        print "Bye!"
        #End program
        break
    else:
        print"Sorry, that wasn't Y or N. Try again."
like image 150
Not_a_Golfer Avatar answered May 05 '26 23:05

Not_a_Golfer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!