【问题标题】:If statements & else not working as intended [closed]If 语句和 else 没有按预期工作[关闭]
【发布时间】:2020-07-26 19:35:14
【问题描述】:
    if b1a == "rock":
    print("you gave him a concussion and made the rock your new weapon")
    weapon = "rock"
else:
    print("you had a heart attack because you didn't pick one of the options")
    exit()

if b1a == "NIGERUNDAYO":
    weapon = "tommy gun"
    print("You ran so much that the beast got tired, then you took out a tommy gun and started shooting")
else:
    print("you had a heart attack because you didn't pick one of the options")
    exit()

if b1a == "slash":
    print("you slashed his face with a hatchet. ")
else:
    print("you had a heart attack because you didn't pick one of the options")
    exit()

它总是让我心脏病发作。

我是初学者,想知道是否有其他方法可以解决这个问题。

【问题讨论】:

  • 你需要elif
  • 如果您是初学者,那么在这种情况下阅读一些有关 Python 的基础教程可能是一个很好的主意。您似乎遇到的问题应该被他们和许多其他有用的东西所涵盖。

标签: python python-3.x


【解决方案1】:

最好每次只使用elif 而不是if-else 构造。

if b1a == "rock":
    print("you gave him a concussion and made the rock your new weapon")
    weapon = "rock"
elif b1a == "NIGERUNDAYO":
    weapon = "tommy gun"
    print("You ran so much that the beast got tired, then you took out a tommy gun and started shooting") 
elif b1a == "slash":
    print("you slashed his face with a hatchet. ")
else:
    print("you had a heart attack because you didn't pick one of the options")
    exit()

【讨论】:

    【解决方案2】:

    也许这个解决方案就足够了。

    试试elif statements

    if b1a == "rock":
        print("you gave him a concussion and made the rock your new weapon")
        weapon = "rock"
    
    elif b1a == "NIGERUNDAYO":
        weapon = "tommy gun"
        print("You ran so much that the beast got tired, then you took out a tommy gun and started shooting")
        exit()
    
    elif b1a == "slash":
        print("you slashed his face with a hatchet. ")
    
    else:
         print("you had a heart attack because you didn't pick one of the options")
         exit()
    

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 2022-11-20
      • 2020-03-11
      • 1970-01-01
      • 2016-12-12
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多