2017-06-21 16 views
2

ここでsozを開始してください。私は数字を入力することによっていくつかのオプション(def logged() :)を持つメニューを作ろうとしてきました。そして、それはその機能に目的を持ってジャンプします。しかし、私は指定された関数をwhileループに入れたifステートメントで呼び出すことはできません。代わりに、ログされた関数がwhileループを永続的に実行し続けることになっている場合は、menu()関数に戻ります。Python:whileループ中に関数を呼び出すことができません

logged()のメニューに対応する番号を入力すると、その特定の機能が呼び出されるはずですが、最初のメニューに戻るだけです。私はちょうど前後にジャンプすることなく、2つのメニューが永遠にループするように見えることはできません。どのようにして正確に2つのwhileループを永遠に別々にループさせ、お互いにループさせないのですか?

def menu(): 
    mode = input("""Choose options:\n 
    a) Test1 Calls logged() function 
    b) Test2 
    Enter the letter to select mode\n 
    > """) 
    return mode 

def test1(): 
    print("Test1") 
    logged() 

def test2(): 
    print("Test2") 

def logged(): #Logged menu is supposed to run through a while loop and not break out when reached. 
    print("----------------------------------------------------------------------\n") 
    print("Welcome user. ") 
    modea = input("""Below are the options you can choose:\n 
    1) Function1 
    2) Function2 
    3) Function3 
    4) Exit 
    \n 
    Enter the corresponding number 
    > """).strip() 
    return modea 

def funct1(): #EXAMPLE FUNCTIONS 
    print("Welcome to funct1") 


def funct2(): 
    print("Welcome to funct2") 


def funct3(): 
    print("Welcome to funct3") 

#Main routine 
validintro = True 
while validintro: 
    name = input("Hello user, what is your name?: ") 
    if len(name) < 1: 
     print("Please enter a name: ") 
    elif len(name) > 30: 
     print("Please enter a name no more than 30 characters: ") 
    else: 
     validintro = False 
     print("Welcome to the test program {}.".format(name)) 

#The main routine 
while True: 
    chosen_option = menu() #a custom variable is created that puts the menu function into the while loop 

    if chosen_option in ["a", "A"]: 
     test1() 

    if chosen_option in ["b", "B"]: 
     test2() 

    else: 
     print("""That was not a valid option, please try again:\n """)  

while True: 
    option = logged() 
    if option == "1": 
     funct1() 

    elif option == "2": 
     funct2() 

    elif option == "3": 
     funct3() 

    elif option == "4": 
     break 
    else: 
     print("That was not a valid option, please try again: ") 

print("Goodbye") 

答えて

2

大丈夫ですので、大したことではなく、いくつかの間違いを犯しました。みんなどこかで勉強しなければなりません。

最大の問題は、メニューループ(2番目のwhileループがあります)に入ることですが、終了するために何もしないことです。私がコメントした他にもいくつかの変更点があります。私はあなたが何処で行っていたかを100%確信していませんでしたが、...

私はthis is what you were going for thoughと思っています。私はそれが意図だと思ったので、ちょっと変わったことがあった。

def menu(): 
    mode = input("""Choose options:\n 
    a) Test1 Calls logged() function 
    b) Test2 
    Enter the letter to select mode\n 
    > """) 
    return mode 

def test1(): 
    print("Test1") 
    logged() 

def test2(): 
    print("Test2") 

def logged(): #Logged menu is supposed to run through a while loop and not break out when reached. 
    print("----------------------------------------------------------------------\n") 
    print("Welcome user. ") 
    modea = input("""Below are the options you can choose:\n 
    1) Function1 
    2) Function2 
    3) Function3 
    4) Exit 
    \n 
    Enter the corresponding number 
    > """).strip() 
    return modea 

def funct1(): #EXAMPLE FUNCTIONS 
    print("Welcome to funct1") 


def funct2(): 
    print("Welcome to funct2") 


def funct3(): 
    print("Welcome to funct3") 

#Main routine 
validintro = False # I like it this way 
while not validintro: 
    name = input("Hello user, what is your name?: ") 
    if len(name) < 1: 
     print("Please enter a name: ") 
    elif len(name) > 30: 
     print("Please enter a name no more than 30 characters: ") 
    else: 
     validintro = True 
     print("Welcome to the test program {}.".format(name)) 

#The main routine 
validintro = False # need a way out 
while not validintro: 
    chosen_option = menu() #a custom variable is created that puts the menu function into the while loop 
    validintro = True # start thinking we're okay 
    if chosen_option in ["a", "A"]: 
     test1() # you're calling this, which calls the logged thing, but you do nothing with it 
     # I just left it because I figured that's what you wanted 

    elif chosen_option in ["b", "B"]: # You want an elif here 
     test2() 

    else: 
     print("""That was not a valid option, please try again:\n """) 
     validintro = False # proven otherwise 

validintro = False 
while not validintro: 
    validintro = True 
    option = logged() 
    print(option) 
    if option == "1": 
     funct1() 

    elif option == "2": 
     funct2() 

    elif option == "3": 
     funct3() 

    elif option == "4": 
     break 
    else: 
     print("That was not a valid option, please try again: ") 
     validintro = False 

print("Goodbye") 
+1

原則として、コードを含まない回答は投稿しないでください。何らかの理由でリンクがなくなると、あなたの答えは無益になります。まだrepl.itにリンクすることはできますが、ここでコードをコピーしてください。 – Tomalak

+0

@Tomalak私は良い友達を得ます。 –

1

問題は、あなたのコードは、あなたが欲しいの流路をたどる(上記のコードを試してみて、それはあなたが望むなら、私はそれについて少し考えてよ見て、私が何をしたか説明しようとdoesntのことです今のところ私は関数whileloop()を作成し、正しい場所に追加しました)。

def whileloop(): 
    while True: 
    option = logged() 
    if option == "1": 
     funct1() 

    elif option == "2": 
     funct2() 

    elif option == "3": 
     funct3() 

    elif option == "4": 
     break 
    else: 
     print("That was not a valid option, please try again: ") 

print("Goodbye") 
def menu(): 
    mode = input("""Choose options:\n 
    a) Test1 Calls logged() function 
    b) Test2 
    Enter the letter to select mode\n 
    > """) 
    return mode 

def test1(): 
    print("Test1") 
    whileloop() 

def test2(): 
    print("Test2") 
    whileloop() 

def logged(): #Logged menu is supposed to run through a while loop and not break out when reached. 
    print("----------------------------------------------------------------------\n") 
    print("Welcome user. ") 
    modea = input("""Below are the options you can choose:\n 
    1) Function1 
    2) Function2 
    3) Function3 
    4) Exit 
    \n 
    Enter the corresponding number 
    > """).strip() 
    return modea 

def funct1(): #EXAMPLE FUNCTIONS 
    print("Welcome to funct1") 


def funct2(): 
    print("Welcome to funct2") 


def funct3(): 
    print("Welcome to funct3") 

#Main routine 
validintro = True 
while validintro: 
    name = input("Hello user, what is your name?: ") 
    if len(name) < 1: 
     print("Please enter a name: ") 
    elif len(name) > 30: 
     print("Please enter a name no more than 30 characters: ") 
    else: 
     validintro = False 
     print("Welcome to the test program {}.".format(name)) 

#The main routine 
while True: 
    chosen_option = menu() #a custom variable is created that puts the menu function into the while loop 

    if chosen_option in ["a", "A"]: 
     test1() 

    if chosen_option in ["b", "B"]: 
     test2() 

    else: 
     print("""That was not a valid option, please try again:\n """) 

私は何が起こったかを考えました。あなたのコードが通過している流れを簡単にリストアップしてください。

  1. ループを入力します。while validintro;
  2. while Trueループ(chosen_option = menu()
  3. menu()を開始し、test1()を開始し、logged()を開始し、現在のものですlogged()
  4. を呼び出しtest1()
  5. を呼び出して入力します。 Whileループでtest1()関数を呼び出すと、実行フローが戻ります。
  6. if chosen_option in ['b', 'B']と入力します。
  7. 内部ではないので、elseステートメントを有効にして、エラーメッセージを表示します。その後、ループが再開されます。
関連する問題