print("... Welcome to the Calculator...")
SolveType = input("Do you want me to Multiply, Add, Subtract or Divide?")
CalculationInput = int(input("What do you want me to solve? Enter the first number: "))
CalculationInput2 = int(input("Enter your second number: "))
division = CalculationInput / CalculationInput2
sum = CalculationInput + CalculationInput2
multiplication = CalculationInput * CalculationInput2
subtraction = CalculationInput - CalculationInput2
prefix_foraddition = ["add","Add","Addition","addition","+"]
prefix_forsubtraction =["subtract","subtraction", "Subtract", "Subtraction", "Minus","minus","-"]
prefix_divison = ["Division","divide","/"]
prefix_formultiplication = ["Multiply","multiply","multiplyby","times","*"]
try:
if SolveType == prefix_foraddition:
print("The Sum is ... " , sum)
elif SolveType == prefix_foraddition:
print("The Sum is ....",subtraction)
elif SolveType == prefix_divison:
print("The quotient is ... ", division)
elif SolveType == prefix_formultiplication:
print("The product is ... ",multiplication)
else:
print("Invalid Input ")
except ValueError:
print("Error") ``` so You notice how theres like a certain exception I'm looking for right; I purposefully cause that exact exception and I dont get the print statement
#๐ It's not printing the print statement with the exception
57 messages ยท Page 1 of 1 (latest)
Hey @clear canopy!
It looks like you incorrectly specified a language for your code block.
Make sure you put your code on a new line following python. There must not be any spaces after python.
Here is an example of how it should look:
```python
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
@clear canopy
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.
Idk why it pasted that way rip
oh
What would you say is wrong with it
a lot
What in here
if SolveType == prefix_foraddition:
print("The Sum is ... " , sum)
elif SolveType == prefix_foraddition:
print("The Sum is ....",subtraction)
elif SolveType == prefix_divison:
print("The quotient is ... ", division)
elif SolveType == prefix_formultiplication:
print("The product is ... ",multiplication)
else:
print("Invalid Input ")
would cause a ValueError?
but like I still take the input though, I calculate everything in advanced cause I didn't know what to use to detect if the user wanted to add smth if yk what I mean
Nah like if the input was for example 5 and the next input was like s or a string
wait
try-except only catches errors that occur inside the try: block
you check if the solvetype is equal to the list
thats not how you do that
you want in
!e
try:
int("not a number")
except ValueError:
print("oh no")
:white_check_mark: Your 3.12 eval job has completed with return code 0.
oh no
!e
int("not a number")
try:
print("something unrelated")
except ValueError:
print("oh no")
:x: Your 3.12 eval job has completed with return code 1.
001 | Traceback (most recent call last):
002 | File "/home/main.py", line 1, in <module>
003 | int("not a number")
004 | ValueError: invalid literal for int() with base 10: 'not a number'
Ima try this
So is there any way to prevent an error/exception based on the input of the user and not really the code
What do you mean?
`print("... Welcome to the Calculator...")
SolveType = input("Do you want me to Multiply, Add, Subtract or Divide? ")
CalculationInput = int(input("What do you want me to solve? Enter the first number: "))
CalculationInput2 = int(input("Enter your second number: "))
division = CalculationInput / CalculationInput2
sum_result = CalculationInput + CalculationInput2
multiplication = CalculationInput * CalculationInput2
subtraction = CalculationInput - CalculationInput2
prefix_foraddition = ["add", "Add", "Addition", "addition", "+"]
prefix_forsubtraction = ["subtract", "subtraction", "Subtract", "Subtraction", "Minus", "minus", "-"]
prefix_divison = ["Division", "division", "Divide", "divide", "/"]
prefix_formultiplication = ["Multiply", "multiply", "Multiplyby", "multiplyby", "times", "Times", "*"]
try:
if SolveType in prefix_foraddition:
print("The Sum is ...", sum_result)
elif SolveType in prefix_forsubtraction:
print("The Difference is ...", subtraction)
elif SolveType in prefix_divison:
print("The Quotient is ...", division)
elif SolveType in prefix_formultiplication:
print("The Product is ...", multiplication)
else:
print("Invalid Input")
except ValueError:
print("Error")` try this
try:
int(user_input)
except ValueError:
pass # user input bad
you're not really 'preventing' an error you're just deciding what to do with it.
like if I divide5 by s instead of this
Traceback (most recent call last):
File "c:\Users\dzmon.vscode\Basic_Calculator.py", line 4, in <module>
CalculationInput2 = int(input("Enter your second number: "))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 10: 's'
it would print something I put
yeah
I worded it wrongly mb
put that in the except block
you'll have to make it more robust than how you currently have it...
If you want to catch an error coming from int(...) in this line, then this line should go under the try: block. Not some different line
print("The Sum is ... " , sum)
elif SolveType == prefix_foraddition:
print("The Sum is ....",subtraction)
elif SolveType == prefix_divison:
print("The quotient is ... ", division)
elif SolveType == prefix_formultiplication:
print("The product is ... ",multiplication)
else:
print("Invalid Input ")
try:
int(CalculationInput,CalculationInput2)
except ValueError:
pass # user input bad```
does this look fine?
Hey @clear canopy!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
๐ญ
that's not how int works
and that doesn't do what you want
you already calculated the inputs before
please don't copy-paste
type it yourself
ok
When you enter a non-integer, what line does the error traceback list? It would be one of your int(input(".....")) lines, yes?
That is where the exception occurs, and therefore that is what needs to be inside a try/except.
... because it is the int() call in that line which raises the exception, trying to read your input string as an integer.
thanks man
I finally got it to work
I did this then it led me to find out apparently theres a chance for exceptions in other places in the code so I put most of the code under try and added the exception
and it ended up working
!close
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.