Like-
A= input()
B= input()
print(int(A) - int(B))
My question is if someone press enter key without putting anyno. It shows error.
Is thier any way that when someone press enter key like on a, then it put value of A as zero And If someone press enter key on b, then it put value of B as zero
Plz help me out..
CodePudding user response:
If you just want to turn the empty string into 0
, test for it.
A = input()
B = input()
a = int(A) if A else 0
b = int(B) if B else 0
print(a b)
But if you want to be more forgiving of bad input data, you could try to make an integer out of the input and catch the error.
A = input()
B = input()
def int_or_zero(val):
try:
return int(val)
except ValueError:
print(repr(val), "didn't work, using 0")
return 0
print(int_or_zero(A) - int_or_zero(B))
CodePudding user response:
Input returns an empty string if you press "enter". It's easy to check for that.
A = input("Enter one")
B = input("Enter two")
A = int(A) if A else 0
B = int(B) if B else 0
print(A-B)