#πŸ”’ Calculator in python review

33 messages Β· Page 1 of 1 (latest)

thorn frigate
#
import time

#------------MENU-------------------------
print("welcome to hertz calculator!")
num1=int(input("enter first number "))
num2=int(input("enter second number "))
print("Choose the operation \n1.+\n2.-\n3.Γ·\n4.x")
op=input()
#-------------------functions---------------------------
def add():
    print("Result is",num1+num2)
def min():
    print("Result is",num1-num2)
def devi():
    print ("Result is",num1/num2)
def multi():
    print("Result is",num1*num2)
#-------------------Choice----------------------------
if op == "1":
    add()
elif op =="2":
    min()
elif op =="3":
    if num2 == 0:
        print("Result is undefined")
    else:
        devi()
elif op =="4":
    multi()
else:
    print("Error, it Must be From 1 To 4")
end=input("press enter to exit ")

Thanks in advance πŸ˜„

south tinselBOT
#

Hey @thorn frigate!

Please edit your message to use a code block

Add a py after the three backticks.

```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
south tinselBOT
#

@thorn frigate

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

jagged raft
#

I'd add num1 and num2 as parameters to each function.

def add(x,y):
    print("Result is: " + str(x+y) )

#restOfCode

if op=='1':
    add(num1,num2)

thorn frigate
jagged raft
#

Just the square root of 1 of num1?

thorn frigate
#

Ye just the user select the root and the app will use root in num1 not num2

#

Or just create a def that requires one number ?

#

And the other one reqiues two numbers for + and Γ— etc

jagged raft
#

You could use

if op == '5':
    print(num ** 0.5)
#

Or use the math module's sqrt()

thorn frigate
#

Like import math ?

jagged raft
#

I think so?

import math as mt

x = mt.sqrt(25)
#

I'm 98% sure it's a default cpython lib

thorn frigate
jagged raft
#

Yes

thorn frigate
jagged raft
#

CPython is like the "default" implementation of the python programming language. As opposed to PyPy or Jython.

thorn frigate
#

Key

#

Thank you

proven snow
#

nice calculator. if you'd like, you could protect against errors due to bad input - right now your code runs int(input()). if the user inputted a non-number, like, "banana", it would break. you could run a try/except check to prevent?

#

while True:
  number = input()
  try: 
    number = int(number)
    break #this exits the loop
  except ValueError:
    print("that's not a valid number!")
jagged raft
#

Funnily it will work for addition if the user puts in two strings.

proven snow
rigid anchor
proven snow
sudden burrow
#

Why do you need to import time since is not used anywhere?

sudden burrow
lilac sage
#

the while loop only gets one number. The point is that the user can repeatedly fail to input a valid number

#

(any number of times, before smartening up)

south tinselBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.