【问题标题】:Break loop and start loop again中断循环并重新开始循环
【发布时间】:2020-11-14 08:26:59
【问题描述】:
col1 = input('Please input the first color: ')
col2 = input('Please input the second color: ')

while True:

    if (col1 == 'red' and col2 == 'blue')  or (col1 == 'blue' and col2 == 'red'):
        print('purple')
    elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
        print('orange')
    elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
        print('green')
    if col1 not in ['red', 'blue', 'yellow']:
        print('Invalid')
        break 

我试图让用户回答“红色”、“蓝色”或“黄色”。如果他们没有在该列表中回答,程序将打印 ('invalid') 并通过再次询问用户混合哪 2 种颜色来启动循环。

【问题讨论】:

  • 我希望你的输入命令在循环内,当你得到有效输入时你应该跳出循环,而不是当你得到无效输入时。
  • 我当然会这样做。它还没有解决问题。只有一个接近我想要的输出。谢谢你让我知道:)
  • 如果它不能解决你的问题,让作者知道,他们可能会相应地更新答案。此外,如果它足够接近,您也许能够弄清楚其余部分。

标签: python


【解决方案1】:
colors = { 
    ("blue", "red") : "purple",
    ("red", "yellow") : "orange",
    ("blue", "yellow") : "green",
}

def mix(color1, color2):
    c = tuple(sorted((color1, color2)))
    return colors.get(c)
    
c1 = input("First color: ")
c2 = input("Second color: ")

while not (m := mix(c1, c2)):
    print("Invalid")
    c1 = input("First color: ")
    c2 = input("Second color: ")
    
print(m)

第一种颜色:a

第二种颜色:b

无效

第一种颜色:蓝色

第二种颜色:红色

紫色

无需多次if-else 检查。

【讨论】:

  • 不错的答案。请注意 := 仅适用于 python >= 3.8 link
  • 谢谢!我可能忘了提到如果第一种颜色不是所需的混合,我希望程序打印“无效”。很棒的代码,它教会了我改进的新方法:D
【解决方案2】:

我假设您想在输入有效时退出循环,并且循环将再次运行,直到输入有效为止。

为此,请稍微修改您的代码,如下所示。 这里发生的是,当您运行程序时,它会直接进入循环并询问用户输入。输入后它会评估,如果它们有效,它会中断并移出循环。但如果输入无效,它将进入下一次迭代并再次询问输入。


while True:
    col1 = input('Please input the first color: ')
    col2 = input('Please input the second color: ')
    if (col1 == 'red' and col2 == 'blue')  or (col1 == 'blue' and col2 == 'red'):
        print('purple')
        break
    elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
        print('orange')
        break
    elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
        print('green')
        break
    if col1 not in ['red', 'blue', 'yellow']:
        print('Invalid')

【讨论】:

    【解决方案3】:

    这应该可行:

    VALID_ARGUMETNS = ['red', 'blue', 'yellow']
    
    
    def user_input():
        col1 = input('Please input the first color: ')
        col2 = input('Please input the second color: ')
        return col1, col2
    
    
    if __name__ == '__main__':
        col1, col2 = user_input()
    
        while col1 not in VALID_ARGUMETNS or col2 not in VALID_ARGUMETNS:
            print('Invalid, try again:')
            col1, col2 = user_input()
    
        if (col1 == 'red' and col2 == 'blue') or (col1 == 'blue' and col2 == 'red'):
            print('purple')
        elif (col1 == 'red' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'red'):
            print('orange')
        elif (col1 == 'blue' and col2 == 'yellow') or (col1 == 'yellow' and col2 == 'blue'):
            print('green')
    

    【讨论】:

      【解决方案4】:

      在这种情况下,break 是多余的。每次删除它都会重新开始循环。

      【讨论】:

        【解决方案5】:

        在这个程序中使用循环有点复杂,这是正确的,但与它所做的相比,它使程序更加复杂。像这样的简单代码应该可以工作 -

        def col():
            col1 = input('Please input the first color: ')
            col2 = input('Please input the second color: ')
            a=['red','blue','yellow']
            if col1 in a and col2 in a:
                if (col1=='red' and col2=='blue')  or (col1=='blue' and col2=='red'):
                    print('purple')
                elif (col1=='red' and col2=='yellow') or (col1=='yellow' and col2=='red'):
                    print('orange')
                elif (col1=='blue' and col2=='yellow') or (col1=='yellow' and col2=='blue'):
                    print('green')
            else:
                print('Invalid')
                col()
        col()
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-30
          • 2012-02-04
          • 2020-09-05
          • 2021-12-18
          • 2017-04-29
          • 2022-10-14
          • 1970-01-01
          相关资源
          最近更新 更多