+ 2
Asserting inputs in py
How can we assert an input type as int ? And also if its not how can we try to get another input ?
5 Antworten
+ 6
while True:
try:
s = input()
assert s.isnumeric()
break
except AssertionError:
print('enter a number')
+ 3
while True:
try:
inp = int(input())
break
except:
continue
+ 2
Anna 's code doesn't work for negative integers, this combines the same idea, but also works for negative integers:
while True:
try:
print("Enter any integer, please.")
put = input(": ")
int(put)
except ValueError:
print("Invalid input!")
continue
else:
break
0
Seb TheS you can also just do this
put = int(input(": "))
0
If you want to collect input = input()
*Attach to variable or var
If you want to collect only numbers = int(input())
*Attach to variable or vr