Asserting inputs in py | Sololearn: Learn to code for FREE!


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 ?

6/12/2019 8:09:33 AM

Amir Motamedi

5 Answers

New Answer


while True: try: s = input() assert s.isnumeric() break except AssertionError: print('enter a number')


while True: try: inp = int(input()) break except: continue


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


Seb TheS you can also just do this put = int(input(": "))


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