2016-11-20 33 views
0

因此,我正在爲我的程序製作一個輸入菜單,該菜單運行某些功能以根據用戶輸入的內容顯示數據。用戶必須按順序輸入選擇;如果用戶在2之前的1或3之前選擇2,則程序應報告錯誤。直到我添加了描述用戶錯誤的錯誤字符串時,我才正常工作。另外,之前我還沒有弄清楚如何解決的一個問題是,當輸入錯誤的選擇時,選擇之前生成的數據必須仍在使用中(即選擇1中生成的數據必須仍然可用,如果選擇3代替輸入,然後使用選擇2)。但是,我的程序沒有效仿。我想知道如果我能得到一些關於如何修改我的代碼的提示。輸入菜單Python 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) 

注:我知道,在else語句的東西不是做得比較工作。這是一個朋友建議狗屎和演出。想知道如果我能得到對幫助過,但它不是沒有必要像我大概可以算出它

回答

0

所以,基本上這是怎麼回事是cond1cond2每個while(run):塊貫穿時間重置。

我把你的代碼在沙箱中,這是修復(我修改的最後else塊以及):

'''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

哦,我完全沒聽懂。非常感謝!! – schCivil