【问题标题】:Function not being used when called [duplicate]调用时未使用函数[重复]
【发布时间】:2020-10-11 20:33:35
【问题描述】:

这是一个 Python 游戏的基本战利品表。

  1. 如果投掷在任何数字上滚动到 13(包括 13),则很常见
  2. 如果掉落从 14 到 18 并在 18 上滚动,则不常见
  3. 如果掉落在 19 或 20,则稀有

即使满足条件,程序也会忽略稀有掉落表,而是从罕见掉落表中取出物品,例如 drop = 20 1000 金币

有人能解释一下这里的问题吗?

import random


player_won = True

def common():
    common_drop= ['Dragon Bones','Dragon Hide','Dragon Teath','Steel Scimitar','Iron Arrows','Bronze Kitesheild']
    print(random.choice(common_drop))
    

def uncommon():
    uncommon_drop= ['Rune Platebody','1000 Gold Coins','Dragonstone','Rune longsword','Mithril Javelin','Rune Med Helm']
    print(random.choice(uncommon_drop))
    
    
def rare():
    rare_drop= ['Dragon Head','Gold Trimmed Rune Full Helm','Crystal Halberd','Diamond (uncut)']
    print(random.choice(rare_drop))    

while player_won == True:
    drop = random.randint(1,20)
    print(drop)
    if drop <= 13:
        common() 
    elif drop > 13 <= 18:
        uncommon()
    elif drop == 19 or 20:
        rare()
    break        

【问题讨论】:

  • 这是因为您的第二个 elif 条件 - 13 始终小于或等于 18。
  • tl;dr 13 &lt; drop &lt;= 18drop in {19, 20}
  • drop &gt; 13 &lt;= 18True &lt;= 18 (因为如果drop &lt;= 13 为真,我们就不会在elif 部分)又名1 &lt;= 18,因此uncommon() 总是被执行(除非drop &lt;= 13) 和 rare() 永远不会被执行。

标签: python python-3.x function while-loop


【解决方案1】:

你需要在不常见的 elif 中移动你的 drop 变量;所以看起来像

elif 13 < drop <= 18:

【讨论】:

  • rareelif也有问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-06
  • 2014-07-07
  • 1970-01-01
  • 2012-08-11
  • 2018-02-14
  • 2017-01-22
相关资源
最近更新 更多