4

私は現在Pythonを学んでいて、何か疑問に思っていました。私は小さなテキスト アドベンチャー ゲームを書いていますが、これについて助けが必要です: たとえば、

example = input("Blah blah blah: ")
if example <= 20 and > 10:
    decision = raw_input("Are you sure this is your answer?: ")

"example = input("Blah blah blah: ")" を再び実行させるために、どのような関数を書くことができますか? ユーザーが "decision = raw_input("Are you sure this is your answer?: ")" にノーと言った場合。

皆様を混乱させてしまい申し訳ありません。私はPythonの初心者であり、プログラミングはまったくの初心者です。

4

2 に答える 2

4

whileあなたはループを探しています:

decision = "no"
while decision.lower() == "no":
    example = input("Blah blah blah: ")
    if 10 < example <= 20:
        decision = raw_input("Are you sure this is your answer?: ")

ループは、条件が成立しなくなるまでコード ブロックを繰り返し実行します。

最初に決定を設定して、少なくとも 1 回実行されるようにします。明らかに、 よりも優れたチェックを実行したい場合がありますdecision.lower() == "no"

if example <= 20 and > 10:また、構文的に意味をなさないため、条件の編集にも注意してください(10 を超えるとは何ですか?)。あなたはおそらく が欲しかったでしょうif example <= 20 and example > 10:。これは に要約できます10 < example <= 20

于 2012-12-09T19:24:18.440 に答える
-1

入力が有効になるまで自分自身を呼び出す関数を使用できます。

def test():
   example = input("Blah blah blah: ")
   if example in range(10, 21): # if it is between 10 and 20; second argument is exclusive
      decision = raw_input("Are you sure this is your answer?: ")
      if decision == 'yes' or desicion == 'Yes':
         # code on what to do
      else: test()
   else: # it will keep calling this until the input becomes valid
      print "Invalid input. Try again."
      test()
于 2012-12-09T19:58:25.677 に答える