Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I have a error

+10 votes
asked Nov 8 by Mayank Venkata Sai Guggilam (220 points)
When I add in this code it does not add

https://onlinegdb.com/kPlY2pxY8

4 Answers

0 votes
answered Nov 9 by Peter Minarik (63,320 points)

Try/except can be used for catching exceptions:

print ('Type in 2 numbers and then choose to add, subtract or multiply them')
try:
    num1 = int(input("Type in the first number: "))
except ValueError:
    print('This code needs a number and not a alphabet')
    num1 = 1

try:
    num2 = int(input("Type in the second number: "))
except ValueError:
    print('This code needs a number and not a alphabet')
    num2 = 1

chose = input('Type in a addition, subtraction or multiplication sign: ')
if chose == '+':
    print(num1 + num2)
elif chose == '-':
    print(f"{num1} - {num2} = ", num1 - num2)
elif chose == '*':
    print (num1 * num2)
else:
    print('The sign you have chosen is not one of the choices'
0 votes
answered Nov 15 by sirisha velpi (140 points)
#check this code

a = 0
print ('Type in 2 numbers and then choose to add, subtract or multiply them')

num = int(input("Type in the first number: "))
if num != int:
    print('This code needs a number and not a alphabet')
    a = 1
    
number = int(input("Type in the second number: "))
if number != int:
    print('This code needs a number and not a alphabet')
    a = 1

chose = input('Type in a addition, subtraction or multiplication sign: ')
if chose == '+':
    print(num + number)
elif chose == '-':
    print(num - number)
elif chose == '*':
    print (num * number)
else:
    print('The sign you have chosen is not one of the choices')
    a = 1
+2 votes
answered Nov 17 by Desmond Asiedu Asamoah (180 points)
Line 13 and Line 18

Replace variable_name with your respective variable names

if type(variable_name) != int: or if not isinstance(variable_name, int):
0 votes
answered 5 days ago by Kamal Lukka (240 points)
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...