Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Python 

Basic Python Calculator. (MY FIRST PYTHON PROJECT THINGY...)

I've been paying a guy to teach me Python.
In 3 hours we've covered a shitload and I just coded this by myself. (he helped me where I failed)
I must say, he's a great teacher.
Teacher: stonerdays

Code:
Code
# Calculator test by Mr.Zetax / ~OMC~
print 'Coded for HackForums by Mr.Zetax / ~OMC~'
print 'Please input 2 numbers and a calculation method, thank you.'
print 'Maximum of 5 calculations per application instance.'
print ''
number1 = int(raw_input('First Number: '))
number2 = int(raw_input('Second Number: '))
method = str(raw_input('Calculation Method: '))

if method == '/':
   print number1/number2
elif method == '+':
   print number1+number2
elif method == '-':
   print number1-number2
elif method == '*':
   print number1*number2
else:
   print 'Invalid method, try again.'

runagainq = raw_input('Would you like to do another calculation? (Y/N)')
if runagainq == 'Y':
   pass

if runagainq == 'N':
   quit()


   # Calculator test by Mr.Zetax / ~OMC~
print 'Please input 2 numbers and a calculation method, thank you.'
print 'Maximum of 5 calculations per application instance.'
print ''
number1 = int(raw_input('First Number: '))
number2 = int(raw_input('Second Number: '))
method = str(raw_input('Calculation Method: '))

if method == '/':
   print number1/number2
elif method == '+':
   print number1+number2
elif method == '-':
   print number1-number2
elif method == '*':
   print number1*number2
else:
   print 'Invalid method, try again.'

runagainq = raw_input('Would you like to do another calculation? (Y/N)')
if runagainq == 'Y':
   pass

if runagainq == 'N':
   quit()


   # Calculator test by Mr.Zetax / ~OMC~
print 'Please input 2 numbers and a calculation method, thank you.'
print 'Maximum of 5 calculations per application instance.'
print ''
number1 = int(raw_input('First Number: '))
number2 = int(raw_input('Second Number: '))
method = str(raw_input('Calculation Method: '))

if method == '/':
   print number1/number2
elif method == '+':
   print number1+number2
elif method == '-':
   print number1-number2
elif method == '*':
   print number1*number2
else:
   print 'Invalid method, try again.'

runagainq = raw_input('Would you like to do another calculation? (Y/N)')
if runagainq == 'Y':
   pass

if runagainq == 'N':
   quit()


   # Calculator test by Mr.Zetax / ~OMC~
print 'Please input 2 numbers and a calculation method, thank you.'
print 'Maximum of 5 calculations per application instance.'
print ''
number1 = int(raw_input('First Number: '))
number2 = int(raw_input('Second Number: '))
method = str(raw_input('Calculation Method: '))

if method == '/':
   print number1/number2
elif method == '+':
   print number1+number2
elif method == '-':
   print number1-number2
elif method == '*':
   print number1*number2
else:
   print 'Invalid method, try again.'

runagainq = raw_input('Would you like to do another calculation? (Y/N)')
if runagainq == 'Y':
   pass

if runagainq == 'N':
   quit()

It works using ELIF's and userinputs.

Enjoy.


NO HATE PLEASE AS IT'S MY FIRST CODE.
I taught OP Python basics in three hours. I know there a lot more easy/advance ways of making this, but it's pretty damn good for someone who started three hours ago.
(May 30, 2014 08:33 PM)stonerdays Wrote:  I taught OP Python basics in three hours. I know there a lot more easy/advance ways of making this, but it's pretty damn good for someone who started three hours ago.
It's obvious why it's so good, right?
I had an amazing teacher!:)
Good job OP...
I just copied and pasted it into my Python IDE and it works great.
I'm positive he's a great teacher but I think I'll stick to my free resources.
Keep it up
Keep going bro you can do better 👍
Does that code have the same lines to much lines and im sleepy
(May 31, 2014 01:41 AM)M.o.n.s.t.e.r Wrote:  Does that code have the same lines to much lines and im sleepy

Yeah, I C+P'd it as I couldn't figure out how to make Y answer loop back to beginning of the code.:(
Why not instead of :
Code
if runagainq == 'Y':
   pass
You make it all a def and you just loop, Like so..
Code
def Anything():
      number1 = int(raw_input('First Number: '))
      number2 = int(raw_input('Second Number: '))
      method = str(raw_input('Calculation Method: '))

      if method == '/':
      print number1/number2
      elif method == '+':
      print number1+number2
      elif method == '-':
      print number1-number2
      elif method == '*':
      print number1*number2
      else:
      print 'Invalid method, try again.'
      runagainq = raw_input('Would you like to do another calculation? (Y/N)')
      if runagainq == 'Y':
      Anything()

      if runagainq == 'N':
      quit()
Anything()
(Warning! doubt you can copy paste this, lines might need a space or back space. didn't fill like writing it in Noptepad++ or pythons default)
So if the users said " if runagainq == 'Y':" putting "Anything()" will loop back to the top.
Tip!
You can do "
Code
if runagainq == 'Y' or == "y"
That way it's going to work with lower or capital (there is a more advanced way but you'll learn)
Hope This Helps !
ARGH!!! Centered! BAD! Indenting is messed up. Never center your code.

As for making it run a bunch of times you're going to need a while loop. Something like:

Code
runAgain = True
while runAgain:
    ...all your code...
    runAgainQ = raw_input('Would you like to do another calculation? (Y/N)')

    if runAgainQ.lower in ('y', 'yes'):
      runAgain = True
    else if runAgainQ.lower in ('n', 'no'):
      runAgain = False

The loop will keep runnng unstil runAgain is set to False - that can only happen if runAgainQ is no.
[Image: QmUOY2P.png]
Code
if runAgainQ.lower in ('y', 'yes'):
      runAgain = True
    else if runAgainQ.lower in ('n', 'no'):
      runAgain = False
From:
Quote:ѕнα∂σω


Also just to note by adding the .lower() the user input will be forced to lower helps with making your if and elif trigger True or False without having a cap error making it trigger your else

Hope this helped, you might already know though
Very basic dude but as you said this is your first code. My first code was also a calculator.
Obviously there are more effective ways to code this out, but not bad for your first project. My first Python project was a calculator too. Keep it up.
Not bad so far, but you should use a loop instead of copying the code several time. Overall you have done a good job. Keep it up and use loops. My first python project was a Calculator as well. As you learn more you can extend to it.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[Python] by Bass_ReevesAug 20, 2023 01:44 AM 9 632 Aug 20, 2023 01:44 AM
Last Post: Lucky4L
[Python] by LSDJul 15, 2020 02:55 AM 2 197 Jul 15, 2020 02:55 AM
Last Post: greasyAlessandra
[C] by Consulting DetectiveMar 3, 2020 04:50 AM 2 160 Mar 3, 2020 04:50 AM
Last Post: Consulting Detective