【问题标题】:my first else condition jumps to the second else我的第一个 else 条件跳转到第二个 else
【发布时间】:2017-07-31 20:38:11
【问题描述】:

所以我试图不让第一个 ELSE 继续,直到它满足要求输入仅包含 1 或 2 或 3 的条件......但它没有这样做......在 1 次打印后它去了到第二个 ELSE ......我在这里做错了什么?

def line_func(): # line to separate
print("==========================================================================") 

def vs1():
print("") # vertical space

def start1():
vs1()
print("=====================================================")
print("Would you like to start? Choose with (y)yes or (n)no.\n=====================================================\n")   
while True:
    start = input("Answer: ")
    vs1()
    if start == "y" or start == "Y" or start == "yes" or start == "YES" or start == "Yes":         
        line_func()
        vs1()

        menu1 = input("How would you like to proceed?\n\n1)Find an enemy to crush\n2)Restart game\n3)Exit game\n\nodgovor: ")
        if menu1 == "1":
            vs1()
            start_round()
        elif menu1 == "2":
            vs1()
            print("================= RESTARTING GAME ===================")
            vs1()
            print("=====================================================")
            print("Would you like to start? Choose with (y)yes or (n)no.\n=====================================================\n")
            continue
            start1()             
        elif menu1 == "3":
            print("Ok see you next time... bye bye :)")       
            break
        else:
            print("You have to choose one of the options to proceed...")

    elif start == "n" or start == "N" or start == "no" or start == "NO" or start == "No":
        print("Ok see you next time... bye bye :)")       
        break
    else:
        print("You have to choose with yes or no...")

start1()

【问题讨论】:

  • 提示:当您将输入更改为 start = input("Answer: ").lower() 时,您可以将第一个 if 减少为 if start == "y" or start == "yes" 并且还可以减少关于 no-Option 的 elif

标签: python python-3.x


【解决方案1】:

试试下面的代码,为我工作:

def line_func(): # line to separate
    print("==========================================================================") 

def vs1():
    print("") # vertical space

def start1():
    vs1()
    print("=====================================================")
    print("Would you like to start? Choose with (y)yes or (n)no.\n=====================================================\n")   
while not start:
    start = input("Answer: ")
    vs1()
    if start == "y" or start == "Y" or start == "yes" or start == "YES" or start == "Yes":
        start = True
        line_func()
        vs1()

        menu1 = input("How would you like to proceed?\n\n1)Find an enemy to crush\n2)Restart game\n3)Exit game\n\nodgovor: ")
        if menu1 == "1":
            vs1()
            start_round()
        elif menu1 == "2":
            vs1()
            print("================= RESTARTING GAME ===================")
            vs1()
            print("=====================================================")
            print("Would you like to start? Choose with (y)yes or (n)no.\n=====================================================\n")
            continue
            start1()             
        elif menu1 == "3":
            print("Ok see you next time... bye bye :)")       
            break
        else:
            print("You have to choose one of the options to proceed...")

    elif start == "n" or start == "N" or start == "no" or start == "NO" or start == "No":
        start = True
        print("Ok see you next time... bye bye :)")       
        break
    else:
        print("You have to choose with yes or no...")
        start = False

start1()

【讨论】:

    【解决方案2】:
    while True:
         line_func()
        vs1()
    
        menu1 = input("How would you like to proceed?\n\n1)Find an enemy to crush\n2)Restart game\n3)Exit game\n\nodgovor: ")
        if menu1 == "1":
            vs1()
            start_round()
        elif menu1 == "2":
            vs1()
            print("================= RESTARTING GAME ===================")
            vs1()
            print("=====================================================")
            print("Would you like to start? Choose with (y)yes or (n)no.\n=====================================================\n")
            continue
            start1()             
        elif menu1 == "3":
            print("Ok see you next time... bye bye :)")       
            break
        else:
            print("You have to choose one of the options to proceed...")
    

    您需要的是另一个while 循环。中断条件取决于您的要求。

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      相关资源
      最近更新 更多