1.11M Members

How to go back to a line of code...?

 
0
 

Hey, everybody, I'm a newbie at Python (2.7.1), and new to DaniWeb as well. and the apparent simplicity of Python got me interested in programming (seriously, the only thing before this was HTML).
Anyway...
I was taking a test and thought that after I was finished I could make an equation calculator. I've got a few simple equations slapped on there, for proof-of-concept, and they work fine. However, I would like to go back to the user input line of code. How do I go about doing this? I've Googled it, and there's no "goto" function like there is in C, I guess.
Well, here's the code.

print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

d=raw_input("choose equation")

while d:

    if d == "1":
        print "a+1"
        e = input("enter a")
        print e + 1
        
    if d == "2":
        print "a+2"
        e = input("enter a")
        print f + 2
        
    if d == "3":
        print "a+3"
        e = input("enter a")
        print g + 3

    if d == "4":
        print "a+4"
        e = input("enter a")
        print g + 4

Remember, guys, totally a newb.

 
1
 

If by user input line of code you mean going back to choosing an equation without having to leave the program then just move

d=raw_input("choose equation")

into the while loop. Maybe throw a 5th option in there that lets you break out of the loop for when you're done and want to quit the program.

Also I've never learned python 2.x, currently learning python3, but I see most people use

int(raw_input('input number: '))

to get the integer instead of

input('input number: ')

.

Another thing you can do if it's any use to you is using try/except to check whether the user input a number or not, that way the program doesn't crash without you wanting it to.

 
0
 

Whoops. I totally screwed up my code on here. I'll edit it to actually work.
Sorry if I weirded you out with the e, f, and g things. Those weren't supposed to be there.

 
0
 

Aw, man... Now I need to do some debugging. Wait a while. I have homework to do, but I want to get this figured out.

 
1
 

I think you can use a while loop to repeat a particlar thing, all you have to do is to set conditions for it, example

while a!=True:
    #execuable statements here
#what you what it to do after it has finished looping

In python there is no goto all there is are two types of loops which are used to achieve whatever thing you need.

 
0
 

I re-wrote my code...

print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

d=raw_input("choose equation")

while d:

    d=raw_input("choose equation")

    if d == "1":
        print "a+1"
        a = input("enter a")
        print a + 1
        
    if d == "2":
        print "a+2"
        a = input("enter a")
        print a + 2
        
    if d == "3":
        print "a+3"
        a = input("enter a")
        print a + 3

    if d == "4":
        print "a+4"
        a = input("enter a")
        print a + 4

    if d=="exit":
        exit()

But now it asks for the equation twice. Also, how would I make the program quit without IDLE asking if the user wants to kill the program?

 
0
 

I re-wrote my code...

print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

d=raw_input("choose equation")

while d:

    d=raw_input("choose equation")

    if d == "1":
        print "a+1"
        a = input("enter a")
        print a + 1
        
    if d == "2":
        print "a+2"
        a = input("enter a")
        print a + 2
        
    if d == "3":
        print "a+3"
        a = input("enter a")
        print a + 3

    if d == "4":
        print "a+4"
        a = input("enter a")
        print a + 4

    if d=="exit":
        exit()

But now it asks for the equation twice. Also, how would I make the program quit without IDLE asking if the user wants to kill the program?

(*facepalm*) Jeez, I should really think of things before I post. It asks for the equation twice ONLY at the start of the program. At that point it's just fine. But it doesn't even need to be the same equation. It's easy enough to get past, it's just annoying. I'll make the final program look neater when I have the coding down.
BTW:

if d=="exit":
        e=raw_input("Do you really want to exit? (Y/N): ")
        if e=="Y":
            exit()
        if e=="N":
            continue

...Me likey. :)

 
0
 

That works, although if you'd ever want to execute any code after the loop, then exit() wouldn't allow you too. If in the future you would like to extend your program to execute statements after the "while" loop then use something like:

if e == "Y":
    break

which at this point does the same thing as exit() because there's no code after the loop.

 
0
 

Good one.

 
0
 

try simplify the logic and what you want to achieve. This could be done in well optimised way.

 
0
 

Keep it up.

 
0
 

try simplify the logic and what you want to achieve. This could be done in well optimised way.

How do you mean? Just write it out as if I was planning a website or something?
I.E., make a map of how I want things to work in simpler terms?

 
0
 

That works, although if you'd ever want to execute any code after the loop, then exit() wouldn't allow you too. If in the future you would like to extend your program to execute statements after the "while" loop then use something like:

if e == "Y":
    break

which at this point does the same thing as exit() because there's no code after the loop.

Thanks, man, that works wonders. :)

 
0
 

Okay, this is all great (and like I said, I'll clean up the print statements and whatnot when I have the code down), but I still have the issue of the program asking for the equation twice. I have to define d, and then use it in the loop. But this causes the raw_input to be displayed twice, and that's kind of annoying.

print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

d=raw_input("choose equation")

while d:

    d=raw_input("choose equation")

    if d == "1":
        print "a+1"
        a = input("enter a")
        print a + 1

...and so on.
Is there a way to bypass the first raw_input since it does absolutely nothing?
I've tried, but apparently I haven't tried hard enough.

 
0
 

Set d anything but empty value meaning false at line 5, for example:

d = "My value is True"

Then reconsider the variable names and read the PEP8 to prepare for the future.

 
0
 

What I think richieking is referring to is some sort of pseudo code, I don't know if you're familiar with the term?

Like he said, just get the logic down of what you want to do, and then think of the best possible solution. Take every chance you get at optimizing your code, e.g. making it faster, cleaner, smaller, etc.

Also to get rid of the first raw_input statement, you can do it like tonyjv suggested, or just delete line 5 altogether and replace "while d:" to "while True:."

Unless of course the way that tonyjv suggested is the standard, if so then you should do it like that.

 
0
 

Set d anything but empty value meaning false at line 5, for example:

d = "My value is True"

Then reconsider the variable names and read the PEP8 to prepare for the future.

Dude, thanks! :icon_cheesygrin:

 
2
 
print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

while True:
	d=int(raw_input("number:" ))
	if d==0:
		break;
	if d == 1:
		print a+1
		
## Then continue from there
 
0
 
print "Equations"

print "a+1 (1), a+2 (2), a+3 (3), a+4 (4)"

while True:
	d=int(raw_input("number:" ))
	if d==0:
		break;
	if d == 1:
		print a+1
		
## Then continue from there

That, my friend, is a beautiful piece of code. Sorry if I seem like a kid in a candy store, but I love seeing things that just... work!
You guys have all been a great help to me. I'll keep you posted on any improvements or issues in my programs.

 
0
 

well you can upvote me a little if you care to.
Thanks

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: