Lost Password?

Go Back   CodeCall Programming Forum > Software Development > Delphi/Python

Delphi/Python Forum for discussing Borland Delphi and Python coding techniques, tips and tricks. Ask your python questions here.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-17-2008, 10:32 PM
nolls nolls is offline
Newbie
 
Join Date: Jan 2008
Posts: 11
Rep Power: 0
nolls is on a distinguished road
Default A small problem in my calc program

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?

PHP Code:
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 
b
    a
=input("First number")
    print 
a
    b
=input("Second number")
    print 
b
    
print add 

Last edited by nolls; 01-17-2008 at 10:39 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 01-18-2008, 12:08 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

Your way of receiving userinput is not right. Look at this:
Code:
option = input("Input-string... ")
# ...
if option == 1:
    # Do something
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-18-2008, 10:18 PM
nolls nolls is offline
Newbie
 
Join Date: Jan 2008
Posts: 11
Rep Power: 0
nolls is on a distinguished road
Default

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?
Code:
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 ()
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-19-2008, 02:47 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

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.
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)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-19-2008, 09:46 PM
nolls nolls is offline
Newbie
 
Join Date: Jan 2008
Posts: 11
Rep Power: 0
nolls is on a distinguished road
Default

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.

Code:
#!/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 )

Last edited by nolls; 01-19-2008 at 09:48 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #6 (permalink)  
Old 01-20-2008, 01:39 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

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:
Code:
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
    # ...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 01-21-2008, 05:57 PM
nolls nolls is offline
Newbie
 
Join Date: Jan 2008
Posts: 11
Rep Power: 0
nolls is on a distinguished road
Default

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

Code:
#!/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 ()
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 01-22-2008, 12:00 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

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?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 01-22-2008, 03:48 PM
nolls nolls is offline
Newbie
 
Join Date: Jan 2008
Posts: 11
Rep Power: 0
nolls is on a distinguished road
Default

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:

Code:
#!/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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 01-23-2008, 01:04 AM
v0id's Avatar   
v0id v0id is offline
Super Mod
 
Join Date: Apr 2007
Location: Denmark
Posts: 1,880
Last Blog:
CherryPy(thon)
Rep Power: 23
v0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to allv0id is a name known to all
Send a message via MSN to v0id
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with Square root and calculator program!!! 123456789asdf C and C++ 10 12-02-2007 04:35 PM
A small Problem Timotay Delphi/Python 2 10-18-2007 11:45 AM
need help with a small program scott0999 General Programming 5 03-01-2007 12:19 AM
A small problem in the output The_Master C and C++ 3 12-13-2006 12:04 PM


All times are GMT -5. The time now is 10:10 AM.

Contest Stats

dargueta ........ 93.00000
John ........ 87.50000
Xav ........ 50.00000
MeTh0Dz ........ 20.00000
gaylo565 ........ 18.00000
Johnnyboy ........ 3.00000

Contest Rules

Ads