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)
#π I made a simple calculator but can i improve?(i am new to python)
51 messages Β· Page 1 of 1 (latest)
@opal bough
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.
Closes after a period of inactivity, or when you send !close.
This first thing I would do is add a final else: to catch an unexpected action value.
i have done it
wait
You don't need to call float() in the calculations because you already did that with the inputs.
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 :) ")
... no else: ?
if you know context free grammars, regex and at least functions you could write a parser
yeah
lets just say i started learning like an hour ago
and i am a engineering student
so no idea about coding
i find it fun
thats why i wanna learn it
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
sorry i didnt get it
so like remove every line from the if statements?
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?
i am just messing around
only posted it for tips
altho i would love to know what is eval and '''ast.literal_eval'''
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)```
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
ahhhhhh but then if i use the wrong thing in Action: it wouldnt tell me that i messed up
forexample my last string is
else :print("Please use +,-,*,/ for it to work :) ")
and eval treats a string as one python expression and evaluates (and executes) it and returns the result
could u please use it on my code to like give an example?
Yeah, that's true. If you used elif it could work though, maybe try that next :)
my code has 3 elif XD
hm?
I'm dumb hang on
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)```
Yeah!
Do you get the gist of this?
no tbh
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
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
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
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.