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

+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 ?

6/12/2019 8:09:33 AM

Amir Motamedi

4 Answers

New Answer

+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(": "))