2017-11-20 13 views
1

私は現在、パイソンクラスを受講中の大学生です。私たちの任務は、このプログラムを関数で作成することです。メイン関数はメニューを呼び出し、メイン関数にループを書き込んで、メニュー関数のユーザー応答に基づいて他の関数にアクセスします。メニュー機能を持つPythonのメイン関数ループが機能していませんか?

私のループが機能しないようです。メニューオプションを選択すると何も起こりません。今のところ、関数の呼び出しをテストするためのprintステートメントがあります。私は関数を書く前にこれが確実に動作するようにしたい。

誰かが、関数を呼び出すためにループをどのように表示するかの例があれば、多くの助けになります。名前の選択に

def GetChoice(): 
    #Function to present the user menu and get their choice 

    #local variables 
    UserChoice = str() 

    #Display menu and get choice 
    print() 
    print("Select one of the options listed below: ") 
    print("\tP\t==\tPrint Data") 
    print("\tA\t==\tGet Averages") 
    print("\tAZ\t==\tAverage Per Zone") 
    print("\tAL\t==\tAbove Levels by Zone") 
    print("\tBL\t==\tBelow Levels") 
    print("\tQ\t==\tQuit") 
    print() 
    UserChoice = input("Enter choice: ") 
    print() 
    UserChoice = UserChoice.upper() 

    return UserChoice 

def PrintData(): 
    print("test, test, test") 

def AverageLevels(): 
    print("test, test, test") 

def AveragePerZone(): 
    print("test, test, test") 

def AboveLevels(): 
    print("test, test, test") 

def BelowLevels(): 
    print("test, test, test") 

def main(): 
    Choice = str() 

    #call GetChoice function 

    GetChoice() 

    #Loop until user quits 

    if Choice == 'P': 
     PrintData() 
    elif Choice == 'A': 
     AverageLevels() 
    elif Choice == 'AZ': 
     AveragePerZone() 
    elif Choice == 'AL': 
     AboveLevels() 
    elif Choice == 'BL': 
     BelowLevels() 


main() 

答えて

3

ループを呼び出します。

あなたは、プログラムを終了するオプションを追加し、以下のように別のelif文を追加したい場合は、次の

elif Choice == "Q": 
    break 

これは、ループを終了し、そのプログラムを終了します。

(モバイルを使用して多くの編集を行います)

+1

ありがとうございましたできます!!わーい! – ChobitsMng

0

あなたのGetChoiceの戻り値を代入する必要があります()関数:

Choice = GetChoice() 

また、あなたがそうのようなあなたのChoice変数を割り当てる必要が

Choice = GetChoice() 
1

、このような行を削除することもできます。

UserChoice = str() 

Pythonでは、変数の型を明示的に指定する必要はありません。

もう一つの小さな提案は、Choice.upper()とコードの最後の値を比較することです。

while True: 
    Choice = GetChoice() 

をして、メニューのための条件は同じインデントに従わなければならない場合:誰かが「P」を入力した場合は、この方法で、それはまだ、次で始まる必要がありPrintData()

関連する問題