1

私はコードを書きましたが、コードには変数があり、最後に人に終了/続行するかどうかを尋ねたいので、最初の質問に戻って続行すると言います。また、最初に質問を何回繰り返すかを尋ねる方法はありますか。Greggy D さんのコードが 150 行を超えているため、コードをアップロードできません。

4

4 に答える 4

2
i = 0

def code_to_repeat():
    # whatever is your code
    print "I was repeated : " + str(i) +" times"

while(True):
    print "Do you want to run the code (Y/N) : "
    stri = raw_input()
    print "\n"
    if stri=="Y":
        i += 1
        code_to_repeat()
    elif stri=="N"
        print "exiting\n"
        break;
    else:
        print "Please Answer Y/N only.\n"
于 2012-05-12T13:05:03.663 に答える
0

私があなたの質問を正しく理解していれば、このようなことがうまくいくかもしれません。

def dostuff():
    ABC = raw_input("Enter Number (q exits): ")

    if(ABC.lower() == 'q'):  #Allow the user to enter q at this point to exit
       return False

    Product = int(raw_input("Enter Product:"))

    #do stuff to print out the cost of these items.

    #We could forgo the next lines and always return True here assuming the user
    #has more input if they didn't input 'q' for 'ABC'.  That's up to you.

    #return True

    print "Do you have more purchases [Y/N]?"
    answer=raw_input()
    return answer.upper() == 'Y'

while dostuff():
    pass

#same as:  
#while True:
#   if(not dostuff()): 
#      break
于 2012-05-12T13:00:16.340 に答える
0

Python では、while ループによって目標を達成できるはずです。次のような例を使用して、問題を解決できます。

while(raw_input()[0] != 'n'):
    print 'to exit print n'
于 2012-05-12T13:09:57.440 に答える
0

あなたの場合、whileループが機能するはずです。

while(raw_input("to exit enter n ")[0] != 'n'):
        print("Doing some work in the loop, until user enters an 'n'.")

raw_input()

ユーザーの入力を求める良い方法であり、次のようなプロンプトを挿入できます

to exit enter n.

ユーザーが改行を押した場合のように、「n」以上を確認する必要があることに注意してください。また、データの単純な解析を実行することも理にかなっており、誰かが n を入力したかどうかに応答するだけでなく、何かを行うことができます。

于 2012-05-13T12:07:11.113 に答える