nombre=str
while nombre==str:
try:
nombre=int(input("give an integer"))
except ValueError:
print("give a number")
nombre=int(input("give an integer"))
Asked
Active
Viewed 30 times
1 Answers
0
- Setting
nombre = str
doesn't seem right, becausestr
is a type and the idea is fornombre
to eventually be an instance of an actual number. - You don't need to repeat yourself inside the loop, as long as the failure results in the
while
condition remaining true.
I think this will work:
from typing import Optional
nombre: Optional[int] = None
while nombre is None:
try:
nombre=int(input("give an integer"))
except ValueError:
print("that's not a valid integer, try again?")
assert nombre is not None

Samwise
- 68,105
- 3
- 30
- 44
-
`typing` seems like massive overkill here – roganjosh Oct 31 '19 at 21:59
-
agree to disagree. `python` without `mypy` is just `thon-my`. – Samwise Oct 31 '19 at 23:31