-1

これは、私が使用したシーケンスを繰り返すときに使用したコードですが、機能していないようです。誰でも問題を確認できますか?コードは通貨コンバーター用です。私はPython 3.3を使用しています

userDoAgain = input("Would you like to use again? (Yes/No)\n")
if userDoAgain == "Yes":
        getChoice()
elif userDoAgain == "No":
        print("Thankyou for using this program, Scripted by PixelPuppet")
        import time
        time.sleep(3)
else:
        print("Error: You entered invalid information.")
        doagain()

編集、これは残りのコードです:

 if userChoice == "1":
 userUSD = float(input("Enter the amount of USD you wish to convert.\n"))


 UK = userUSD * 0.62

 print("USD", userUSD, "= ", UK, "UK")





elif userChoice == "2":
    UK = float(input("Enter the amount of UK Currency you wish to convert.\n"))

    userUSD = UK * 1.62

    print("UK", UK, "= ", userUSD, "USD")


    def doagain():

        userDoAgain = raw_input("Would you like to use again? (Yes/No)\n")
    if userDoAgain == "Yes":
            getChoice()
    elif userDoAgain == "No":
            print("Thankyou for using this program, Scripted by PixelPuppet")
            import time
            time.sleep(3)
    else:
            print("Error: You entered invalid information.")
            doagain()
4

3 に答える 3

2

一般的に言えば、Python で繰り返される制御フローを処理するために再帰を使用することは、悪い考えです。代わりにループを使用する方がはるかに簡単で、問題も少なくなります。doagainしたがって、再実行に関する質問への回答を確実に得られるように関数を定義するのではなく、whileループを使用することをお勧めします。繰り返す大きな関数については、ループも使用することをお勧めします。

def repeat_stuff():
    while True: # keep looping until told otherwise

        # do the actual stuff you want to do here, e.g. converting currencies
        do_stuff_once()

        while True: # ask about doing it again until we understand the answer
            userDoAgain = input("Would you like to use again? (Yes/No)\n")
            if userDoAgain.lower() == "yes":
                break               # go back to the outer loop
            elif userDoAgain.lower() == "no":
                print("Thank you for using this program")
                return              # exit the function
            else:
                print("Error: You entered invalid information.")

yes/入力文字列のチェックを大文字と小文字を区別しないように変更したことに注意してくださいno。これは、よりユーザーフレンドリーな方法です。

于 2013-10-19T21:12:48.247 に答える
-1

おそらく、入力だけではなく、関数「raw_input」を使用する必要があります。

于 2013-10-19T20:49:42.680 に答える