Here's something taken out of my code to check if the value is greater than 0 and if it's a number:
while(1):
n = input("Type a number of rolls to do, to try and get 3 of the same sides in a row.")
if n.isdigit() and int(n) > 0 == True:
n = int(n)
break
else:
print("Select a proper integer.")
Run Code Online (Sandbox Code Playgroud)
For some reason if you enter a value that should stop the loop like 10, it's seen as a wrong integer. Why is that?
Error aside, the "right" way to do this (specifically, without calling int(n) twice), is to simply catch an exception raised by int(n):
while True:
n = input("Type a number...")
try:
n = int(n)
except ValueError:
continue
if n > 0:
break
print("Select a positive integer")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
55 次 |
| 最近记录: |