2016-11-20 1 views
0

그래서 사용자가 입력 한 내용에 따라 데이터를 표시하는 특정 기능을 실행하는 프로그램 용 입력 메뉴를 만들고 있습니다. 사용자는 선택 항목을 순서대로 입력해야합니다. 사용자가 2 이전에 1 또는 3 전에 2를 선택하면 프로그램은 오류를보고해야합니다. 사용자가 잘못한 것을 나타내는 오류 문자열을 추가 할 때까지 제대로 작동했습니다. 또한 이전에 수정해야 할 방법을 찾지 못했는데 잘못된 선택을 입력하면 선택 이전에 생성 된 데이터가 여전히 재생 중이어야합니다 (예 : 선택 1에서 생성 된 데이터는 선택시 계속 사용할 수 있어야합니다. 3이 입력되면 선택 2가 사용됨). 그러나, 내 프로그램은 그것을 모방하지 않습니다. 내 코드를 수정하는 방법에 대한 몇 가지 팁을 얻을 수 있는지 궁금합니다.입력 메뉴 파이썬 3x

def menu(): 

    '''Displays menu for user and runs program according to user commands.''' 
    prompt = """Select one of the following options: 
    1. Generate a new random dataset. 
    2. Calculate least squares for the dataset. 
    3. Calculate the Pearson Correlation Coefficient for the data set and 
    the estimate. 
    4. Quit.\nEnter your selection: """ 
    userInp = "" 
    run = True 
    while(run): 

     userInp = input(prompt)  
     cond1 = False 
     cond2 = False 

     if userInp == '1': 
      #function/program stuff 
      cond1 = True 
      print("Data Generated.\n") 

     elif userInp == '2' and cond1: 
      #function/program stuff 
      cond2 = True 

     elif userInp == '3' and cond1 and cond2: 
      #function/program stuff 

     elif userInp == '4': 
      run = False 

     else: 
      error1 = "Error: no data generated yet"#         
      error2 = "Error: data generated but least squares not completed" 
      print(cond1 * error1 + cond2 * error2) 

참고 :

여기 내 코드 나는 다른 문에 물건이 잘 작동을하지 않습니다 것을 알고있다. 친구와 친구들을위한 제안이었습니다. 아마 그것을 알아낼 수 있기 때문에 나도 그에 도움을받을 수 있지만 필요가 없습니다 수없는 경우 궁금

답변

0

그래서, 기본적으로 여기에서 일어나고있는 것은 cond1cond2while(run): 블록을 통해 실행될 때마다 재설정하는 것입니다.

'''Displays menu for user and runs program according to user commands.''' 
prompt = """ 
Select one of the following options: 
    1. Generate a new random dataset. 
    2. Calculate least squares for the dataset. 
    3. Calculate the Pearson Correlation Coefficient for the data set and the estimate. 
    4. Quit.\nEnter your selection: """ 
userInp = "" 
run = True 
cond1 = False 
cond2 = False 
while(run): 

userInp = input(prompt)  

if userInp == '1': 
    #function/program stuff 
    cond1 = True 
    print("Data Generated.\n") 

elif userInp == '2' and cond1: 
    #function/program stuff 
    cond2 = True 

elif userInp == '3' and cond1 and cond2: 
    #function/program stuff 

elif userInp == '4': 
    run = False 

else: 
    error= '' 
    if(not cond1): 
     error = "Error: no data generated yet"#         
    elif(not cond2): 
     error = "Error: data generated but least squares not completed" 
    print(error) 
+0

오 남자, 나는 완전히 그것을 잡을하지 않았다 :

나는 샌드 박스에 코드를 넣어

, 이것은 수정 (I뿐만 아니라 마지막으로 다른 블록을 수정)입니다. 정말 고맙습니다!! – schCivil