2016-11-20 13 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) 

注:

は、ここに私のコードで私は他の文のようなものはかなりの仕事をしていないことを知っています。それは、友人からの喧嘩とギグの提案でした。私はおそらくそれを把握することができて私もその上の助けを得るが、その必要はないことができなかった場合は疑問に思う

答えて

0

だから、基本的に何をここで起こっていることはcond1cond2while(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