#๐Ÿ”’ It's not printing the print statement with the exception

57 messages ยท Page 1 of 1 (latest)

clear canopy
#
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
vague monolithBOT
#

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.
vague monolithBOT
#

@clear canopy

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.

clear canopy
#

Idk why it pasted that way rip

hasty cedar
#

value error doesn't occur in the try block

#

also I hope you improve this

clear canopy
clear canopy
hasty cedar
#

a lot

azure shale
hasty cedar
#

you calculate everything in advance

#

instead of on demand

#

also look at str.lower

clear canopy
clear canopy
hasty cedar
#

wait

azure shale
hasty cedar
#

you check if the solvetype is equal to the list

#

thats not how you do that

#

you want in

azure shale
#

!e

try:
    int("not a number")
except ValueError:
    print("oh no")
vague monolithBOT
azure shale
#

!e

int("not a number")
try:
    print("something unrelated")
except ValueError:
    print("oh no")
vague monolithBOT
clear canopy
clear canopy
mossy iris
#

`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

hasty cedar
#
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.

clear canopy
# azure shale What do you mean?

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

clear canopy
#

I worded it wrongly mb

hasty cedar
#

put that in the except block

#

you'll have to make it more robust than how you currently have it...

azure shale
clear canopy
# hasty cedar you'll have to make it more robust than how you currently have it...
 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?
vague monolithBOT
#

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.
hasty cedar
#

no

#

this is worse, sorry.

clear canopy
#

๐Ÿ˜ญ

hasty cedar
#

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

clear canopy
#

ok

somber narwhal
#

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.

clear canopy
#

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

vague monolithBOT
#
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.