値が0より大きいかどうか、およびそれが数値であるかどうかを確認するために、コードから取り出したものを次に示します。
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.")
何らかの理由で、10のようにループを停止する値を入力すると、間違った整数と見なされます。何故ですか?
回答 2 件
使用しないでください
==
: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: n = int(n) break else: print("Select a proper integer.")
エラーはさておき、これを行うための「正しい」方法(具体的には、呼び出すことなく)
int(n)
2回)、によって発生した例外を単にキャッチすることですint(n)
: