Hack Forums
[Python] Basic Python Calculator. (MY FIRST PYTHON PROJECT THINGY...) - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] Basic Python Calculator. (MY FIRST PYTHON PROJECT THINGY...) (/showthread.php?tid=4258220)



Basic Python Calculator. (MY FIRST PYTHON PROJECT THINGY...) - ~OMC~ - May 30, 2014

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.



* - stonerdays - May 30, 2014

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.


* - ~OMC~ - May 30, 2014

(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!:)


* - R3X3Y - May 30, 2014

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


* - SIM-1866423 - May 30, 2014

Keep going bro you can do better 👍


* - M.o.n.s.t.e.r - May 31, 2014

Does that code have the same lines to much lines and im sleepy


* - ~OMC~ - May 31, 2014

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


* - tideman2 - May 31, 2014

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 !


* - ѕнα∂σω - May 31, 2014

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.


* - tideman2 - May 31, 2014

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



* - #BORNE - May 31, 2014

Very basic dude but as you said this is your first code. My first code was also a calculator.


* - clark.dll - May 31, 2014

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.


* - Jekwa - May 31, 2014

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.