-6

私のinputCheck関数では、ユーザーの入力が受け入れ可能かどうかを確認した後、印刷メッセージで確認してから別の関数を実行する必要があります-しかし、それは行われず、理由がわかりません-できますか問題を解決する方法についてアドバイスするには?どうもありがとう!

def main():
    print('WELCOME TO THE WULFULGASTER ENCRYPTOR 9000')
    print('==========================================')
    print('Choose an option...')
    print('1. Enter text to Encrypt')
    print('2. Encrypt text entered')
    print('3. Display Encrypted Text!')
    menuChoice()

def menuChoice():
    valid = ['1','2','3']
    userChoice = str(input('What Would You Like To Do? '))
    if userChoice in valid:
        inputCheck(userChoice)
    else:
        print('Sorry But You Didnt Choose an available option... Try Again')
        menuChoice()

def inputCheck(userChoice):
    if userChoice == 1:
        print('You Have Chosen to Enter Text to Encrypt!')
        enterText()
    if userChoice == 2:
        print('You Have Chosen to Encypt Entered Text!')
        encryptText()
    if userChoice == 3:
        print('You Have Chosen to Display Encypted Text!')
        displayText()

def enterText():
    print('Enter Text')

def encryptText():
    print('Encrypt Text')

def displayText():
    print('Display Text')


main()
4

1 に答える 1

3

ユーザーの入力を文字列(str(input('What ...')))に変換しますが、それを。の整数と比較しますinputCheck。にはelseパスがないためinputCheck、「有効な」選択肢を入力しても何も起こりません。

また、Python 2を使用している場合は、使用するのが望ましい方法inputではありませんraw_input(たとえば、python3.xのraw_input()とinput()の違いは何ですか?を参照してください)。

それ以外はmenuChoice、ユーザーが不正な選択を入力するたびに再帰的に呼び出すことは、おそらく悪い考えです。不正な選択を数百回または数千回入力すると、プログラムがクラッシュします(大量のメモリを浪費することは別として)。コードをループに入れる必要があります。

while True:
    userChoice = str(raw_input('What Would You Like To Do? '))
    if userChoice in valid:
        inputCheck(userChoice)
        break
    else:
        print('Sorry But You Didnt Choose an available option... Try Again')
于 2013-03-25T18:43:03.450 に答える