Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

A small problem in my calc program

calculator

  • Please log in to reply
18 replies to this topic

#1 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 17 January 2008 - 08:32 PM

Maybe 3 months ago I tried out a couple beginners tuts for python. I stopped experimenting for awhile and now I want to try and learn more about python. So I am trying to create a calculator program to refresh my knowledge. Ran into a problem early in the code since when I press 1 nothing happens..... can anyone help or suggest anything?


print "Welcome to Nolls's Calculator 1.0"

option=input
print input("Which of the following would you like to do?")
print "1. addition"
print "2. subtraction"
print "3. multiplication"
print "4. division"
if option==1:
def add(a,b):
print a + b
a=input("First number")
print a
b=input("Second number")
print b
print add

  • 0

#2 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 17 January 2008 - 10:08 PM

Your way of receiving userinput is not right. Look at this:
option = input("Input-string... ")
# ...
if option == 1:
    # Do something

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#3 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 18 January 2008 - 08:18 PM

Thanks, got past that problem!

Now I did a test run everything was fine, except that it doesn't go back and re-run it! And I remembered I had to create a loop. I vaguely remember the "while" loop and forget the other. Any suggestions?
print "Welcome to Nolls's Calculator 1.0"

print "1. addition"
print "2. subtraction"
print "3. multiplication"
print "4. division"
print "5. exit"

option = input("Which would you like to do? ")
if option == 1:
    a = input("First number ")
    print a
    b = input("Plus second number ")
    print b
    print a + b
elif option == 2:
    a = input("First number ")
    print a
    b = input("Minus second number ")
    print a - b
elif option == 3:
    a = input("First number ")
    print a
    b = input("Multiplied by second number ")
    print b
    print a * b
elif option == 4:
    a = input("First number ")
    print a
    b = input("Divided by second number ")
    print b
    print a / b
elif option == 5:
    print "Goodbye!"
    exit ()

  • 0

#4 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 19 January 2008 - 12:47 AM

That's right, you need some kind of loop, and a while-loop would be just fine. I see you've a way to exit your program (option 5), and because you have that, it's easy to wrap a loop around the rest of the code.
while True:
    # Everything you want to have in the loop
    # ...
The reason why I talked about your option 5, is because normally a while-loop will run while its condition is true, but because you've an alternative way of quitting, we can simply make the loop run forever (of course, until your alternative way will stop it)
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#5 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 19 January 2008 - 07:46 PM

I ran into another problem. After I created a loop I chose the conditions of the loop to be if option != 0. When I did this I ran a test run and when I picked addition it would run the addition sequence over and over again. It never went back to choosing the choices.

#!/C:Python25/Calc Test.py
print "Welcome to Nolls's Calculator 1.0"

print "1. addition"
print "2. subtraction"
print "3. multiplication"
print "4. division"
print "5. exit"

option = input("Which would you like to do? ")
while option != 0:
    if option == 1:
        a = input("First number ")
        print a
        b = input("Plus second number ")
        print b
        print a + b
    elif option == 2:
        a = input("First number ")
        print a
        b = input("Minus second number ")
        print a - b
    elif option == 3:
        a = input("First number ")
        print a
        b = input("Multiplied by second number ")
        print b
        print a * b
    elif option == 4:
        a = input("First number ")
        print a
        b = input("Divided by second number ")
        print b
        print a / b
    elif option == 5:
        print "Goodbye!"
        exit ()


Any suggestions? (I appriciate all the help v0id :) )
  • 0

#6 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 19 January 2008 - 11:39 PM

Look where you've put the line, where you're receiving the input - outside the loop! You need to put it inside the loop. Another thing; now that you're using conditions, you don't have to use the option 5's exit.

I would do like this:
option = 0
while option != 5:
    option = input("Input... ")
    # Only use your other four options
    # we've already taken care of the fifth
    # one. When 'option' gets a '5', the condition
    # will become false, and the loop will stop
    # ...

  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#7 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 21 January 2008 - 03:57 PM

Figured it out, thanks for the help!

Here is my calculator program if anyone wants to suggest anything to improve it (it doesn't boot up right away, I had to press enter after I executed it to run it, any reason for this?).

#!/C:Python25/Calc Test.py
print "Welcome to Nolls's Calculator 1.0"
option = 0
while option != 5:
    print "1. addition"
    print "2. subtraction"
    print "3. multiplication"
    print "4. division"
    print "5. exit"
    option = input("Which would you like to do? ")
    if option == 1:
        a = input("First number ")
        print a
        b = input("Plus second number ")
        print b
        print a + b
    elif option == 2:
        a = input("First number ")
        print a
        b = input("Minus second number ")
        print a - b
    elif option == 3:
        a = input("First number ")
        print a
        b = input("Multiplied by second number ")
        print b
        print a * b
    elif option == 4:
        a = input("First number ")
        print a
        b = input("Divided by second number ")
        print b
        print a / b
    elif option == 5:
        print "Goodbye!"
        exit ()

  • 0

#8 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 21 January 2008 - 10:00 PM

Read my previous post; you do not need the fifth option anymore, as you're using a loop with conditions. So you can remove the last three lines of code without problems.

What do you mean by it doesn't boot, and you've to press enter?
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#9 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 22 January 2008 - 01:48 PM

About the 5th option, I was confused as to how it would exit if I got rid of the last three lines. Then I realized, if option isn't equal to 5 it will run, and if it doesn't (by pressing 5) it would stop. Yeah, I felt pretty stupid after I realized that.

O and after I took out the last three lines the program didn't have the problem again. What would happen though, is that I run press F5 to run it and it would run, but nothing would happen. As soon as I would press enter, the program would start running. Pretty weird huh?

Also, I think I read this in "Beginning Python- From Novice to Professional", the first line of code makes it into an executable? Is this true, if not, how do you?

Ok here is the FINAL calculator program:

#!/C:Python25/Calc Test.py
print "Welcome to Nolls's Calculator 1.0"
option = 0
while option != 5:
    print "1. addition"
    print "2. subtraction"
    print "3. multiplication"
    print "4. division"
    print "5. exit"
    option = input("Which would you like to do? ")
    if option == 1:
        a = input("First number ")
        print a
        b = input("Plus second number ")
        print b
        print a + b
    elif option == 2:
        a = input("First number ")
        print a
        b = input("Minus second number ")
        print a - b
    elif option == 3:
        a = input("First number ")
        print a
        b = input("Multiplied by second number ")
        print b
        print a * b
    elif option == 4:
        a = input("First number ")
        print a
        b = input("Divided by second number ")
        print b
        print a / b

  • 0

#10 v0id

v0id

    Retired

  • Retired Mod
  • PipPipPipPipPipPipPipPip
  • 2313 posts

Posted 22 January 2008 - 11:04 PM

The first line makes it into executable file, yes, but it doesn't become a binary file like the files on Windows with the extension, .exe. You need to use a third-party program to achieve this. You can use something like Py2exe. I've never used it myself, but it should work. Good luck.
  • 0
If you enjoy reading this discussion and are thinking about commenting, why not click here to register and start participating in under a minute?

#11 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 23 January 2008 - 07:41 PM

Thank you for all of the assistance void. Gladly appreciate it :).
  • 0

#12 nolls

nolls

    CC Newcomer

  • Just Joined
  • PipPip
  • 16 posts

Posted 04 February 2008 - 06:53 PM

Also, what exactly is Python used to create? I am having trouble coming up with some projects to create that will help me get a better understanding of python.

I don't know if this is because I don't know enough yet, but I also don't know what python is mostly used for. Once I do know, it might be easier for me to create some projects/benchmarks for me to acheive. Any help greatly appreciated.

Best regards
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download