2013-03-25 2 views
-6

사용자 입력이 통과 된 후 input.check 함수에서 허용되는 입력이 인쇄 메시지에 의해 확인 된 다음 다른 함수를 실행해야합니다. 그러나 그렇게하지 않으면 그림이 표시되지 않습니다. 왜 그런지 - 문제를 해결하는 방법에 대해 조언을 해 줄 수 있습니까? 많은 감사합니다!사용자 입력 파이썬 확인하기

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() 
+1

질문이 이해가 가지 않습니다. – wRAR

+0

질문을 죄송합니다. – user2166941

답변

3

당신은 문자열 (str(input('What ...')))에 사용자의 입력을 변환하지만 inputCheck의 정수로 비교합니다. inputCheckelse 경로가 없으므로 "유효한"선택 항목을 입력해도 아무런 변화가 없습니다.

input을 사용하는 경우, raw_input은 이동 방법입니다 (예 : What's the difference between raw_input() and input() in python3.x? 참조).

그 외의 경우 사용자가 불법 선택을 할 때마다 재귀 호출 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') 
+0

감사합니다. 답변을 위해 담당자를 제공 할 것이지만 그렇게하기에는 자신이 충분하지 않습니다! – user2166941

+0

언제든지 답변을 수락 할 수 있습니다.) – rainer

관련 문제