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.

Recommended Answers

All 30 Replies

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.

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.

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.

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.

commented: Thanks! +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?

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. :)

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.

Good one.

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

Keep it up.

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?

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. :)

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.

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.

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.

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:

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

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

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

By upvote you mean... (sorry, DaniWeb newb).

By upvote you mean... (sorry, DaniWeb newb).

Hm. I think I got it. The up arrow to the right of your post... right?

look at the right -> there is up and down sign.
click on up.

Too much; C-coding; richieking? ;)

print """Equations
    Enter empty line to finish the program!
"""

while True:
    d = raw_input("Number:" )
    if d is '':
        break
    d = int(d)
    print '{d} + 1 = {answer}'.format(d=d, answer=d+1)
		
## Then continue from there

Too much; C-coding; richieking? ;)

print """Equations
    Enter empty line to finish the program!
"""

while True:
    d = raw_input("Number:" )
    if d is '':
        break
    d = int(d)
    print '{d} + 1 = {answer}'.format(d=d, answer=d+1)
		
## Then continue from there

its still in the blood too much
:)

Why are using if and if, why not try if, elif, that's better syntax wise.

Why are using if and if, why not try if, elif, that's better syntax wise.

Hm. I suppose it is.
I have more questions as well for Python... should we continue using this thread, or should I start a new one for each topic I have?

yep close the thread and then open a new thread. Dont ever mix threads because it does not help other readers in the future.

Why are using if and if, why not try if, elif, that's better syntax wise.

I think I understand why. Anywho, I've implemented if/elif in a new "program" (no, seriously, I can't really call any of my scripting "programs" yet).
Thanks for all your help. I don't know if I should declare this thread "resolved" yet, as I haven't looked at my equation-chooser lately, but I'll let you know if I have anything else.

It is best to ask a simple question (sometimes they escape from simplicity though) and when the question is either answered or does not matter any more, then mark the thread "solved". That is best because it makes it easier for other Daniweb users. Then, when you have a new question (or have waited more than a week or two), you start a new thread, and we are all better able to find that it is a new thread, and help you with that issue

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.