【问题标题】:Nested if-elif loop or if-elif loop with and operator, which is better?嵌套 if-elif 循环或带有 and 运算符的 if-elif 循环,哪个更好?
【发布时间】:2022-07-18 18:02:03
【问题描述】:

就复杂性和可读性而言,哪个是更好的选择:

1.嵌套 if-elif--

if computer == 's':
    if player == 'w':
        comp_win += 1
    elif player == 'g':
        user_win += 1

2。带有 and 运算符的 if-elif--

if comp == 's' and player == 'w':
    comp_win += 1
elif comp == 's' and player == 'g':
    user_win += 1

【问题讨论】:

  • 为什么你认为一个会比另一个复杂?

标签: python python-3.x


【解决方案1】:

我认为这是个人喜好问题;我更喜欢第一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-31
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    相关资源
    最近更新 更多