#πŸ”’ I made a simple calculator but can i improve?(i am new to python)

51 messages Β· Page 1 of 1 (latest)

opal bough
#

This the my code
first_value = float(input("First: "))
second_value = float(input("Second: "))
action = input("Action:(+,-,,/) ")
if action == "+" :
Sum = float(first_value) + float(second_value)
print("Answer:", Sum)
elif action == "-" :
subtract = float(first_value) - float(second_value)
print("Answer:", subtract)
elif action == "
" :
multiply = (float(first_value) * float(second_value))
print("Answer:", multiply)
elif action == "/" :
divide = (int(first_value) / int(second_value))
print("Answer:", divide)

chrome houndBOT
#

@opal bough

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.

visual walrus
#

This first thing I would do is add a final else: to catch an unexpected action value.

visual walrus
#

You don't need to call float() in the calculations because you already did that with the inputs.

opal bough
#

first_value = float(input("First: "))
second_value = float(input("Second: "))
action = input("Action:(+,-,,/) ")
if action == "+" :
Sum = float(first_value) + float(second_value)
print("Answer:", Sum)
elif action == "-" :
subtract = float(first_value) - float(second_value)
print("Answer:", subtract)
elif action == "
" :
multiply = (float(first_value) * float(second_value))
print("Answer:", multiply)
elif action == "/" :
divide = (int(first_value) / int(second_value))
print("Answer:", divide)
print("Please use +,-,*,/ for it to work :) ")

visual walrus
#

... no else: ?

unreal urchin
#

if you know context free grammars, regex and at least functions you could write a parser

opal bough
opal bough
#

and i am a engineering student

#

so no idea about coding

#

i find it fun

#

thats why i wanna learn it

flat phoenix
#

One thing you can do is have a variable result, and then no matter what branch you take on the if statements you can later print("ANSWER: "+result), without having to do it for each operation

opal bough
#

so like remove every line from the if statements?

unreal urchin
#

i mean for a better calulator you could use eval or better ast.literal_eval but i think that is not what you want because you want to code it yourself and not just use a built-in function?

opal bough
#

only posted it for tips

#

altho i would love to know what is eval and '''ast.literal_eval'''

flat phoenix
# opal bough sorry i didnt get it

Like, instead of having

if x:
    something = ...
    print("ANSWER: "+something)
if y:
    something = ...
    print("ANSWER: "+something)
...```
you can have
```py
if x:
    result = ...
if y:
    result = ...

print("ANSWER: "+result)```
unreal urchin
#

ast is a module (it stands for abstract syntax tree) and the function literal_eval from this module treats a given string as an python literal and evaluates it and returns the result

opal bough
#

forexample my last string is

#

else :print("Please use +,-,*,/ for it to work :) ")

unreal urchin
#

and eval treats a string as one python expression and evaluates (and executes) it and returns the result

opal bough
flat phoenix
flat phoenix
#

Oh true

#

Then it'll work

#

No wait

opal bough
#

hm?

flat phoenix
#

I'm dumb hang on

opal bough
#

ok ok

#

tyt

flat phoenix
#

You could check if the input is valid (i.e the else block didn't run) with a boolean flag. Then, only if it's valid, you print result

#
is_valid = True
if condition_1:
     ...
elif condition_2:
    ...
elif condition_3:
    ...
else:
     is_valid = False

if is_valid:
     print("Answer: "+result)```
opal bough
#

i dont know what is a boolean flag

#

is it the true and false thingy?

flat phoenix
#

Yeah!

flat phoenix
opal bough
#

no tbh

unreal urchin
# opal bough could u please use it on my code to like give an example?
inp = input("enter a calculation task: ")
print(f"the result is: {eval(inp)}")

but this is not good, you would have to use regex to check if the input is only a calulation task. and i was wrong you can not use ast.literal_eval for something like: "5 + 7 * 9 - 2 ** 4" or "1 + 2" because it is an expression and not a literal

flat phoenix
#

Ok, let me get a concrete example

is_tasty= True
food = input()
if food == "Pizza":
     ingredients = "flour, cheese"
elif food == "Tacos":
    ingredients= "eggs, flour"
elif food == "steak":
    ingredients= "meat"
else: #If we reach this else we know the food isn't tasty, since the only tasty foods are pizza, tacos and steak
     print("Your food isn't tasty!")
     is_tasty = False

if is_tasty: #If we reached the else clause this will be False, and so we won't print the ingredients 
     print("Ingredients: "+ingredients)```
#

One website I recommend when starting out is pythontutor.com, there you can see for yourself which steps your code takes, in an interactive way, which can really help you reason about the logic of your program

blazing arch
# opal bough no tbh

I'd recommend doing the CS50 Python course from harvard, it is free online, high quality and there is a discord community where you can get help with the coding exercises if you need, it will give you a good solid base with python

chrome houndBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.