0

1 のテキストと 2 の整数以外を入力するかどうかをユーザーに再度確認する方法について、単純で複雑でない回答を検索しました。

これらの入力変数を入力すると、先生が受け入れない複雑な解決策しか見つかりませんでした。

これらの変数を検証する方法について誰かが私に簡単な解決策を提供できれば可能ですか? これまでのところ、検証方法を知っているのは、特定のオプションでのみ機能する「while not in」関数を使用することだけです。Python初心者なので分かりやすく教えてください。ありがとう!:)

1-studentname=input("what is your name?:") 
2-print("what is 10+10?:")
3-studentanswer=int(input("insert answer:"))
4

2 に答える 2

0

.isalphaとを使用できます.isdigit method。例えば、

studentname=input("what's your name) ?
studentname.isalpha()

これにより、文字列がアルファベット文字のみで構成されているかどうかがチェックされます。両方ともブール値.isalpha.isdigit返すため、if 条件を使用できます。

于 2016-03-17T20:41:55.033 に答える
0

if 、 else で while ループを使用できます

while(true):
   t = int(input());
   if t == 1:
     # do whatever --> break at the end
   else if t == 2:
     # do whatever ---> break at the end
   else:
     continue
于 2016-03-17T20:38:44.593 に答える