【问题标题】:Python - why is my random number generator not working?Python - 为什么我的随机数生成器不起作用?
【发布时间】:2015-02-13 17:03:23
【问题描述】:

我已经尝试了很多年如何让它发挥作用。它应该选择一个带有'from random import randrange'的随机数,然后是'x = randrange(1,3)。我在其他时候使用过那个生成器,然后它就可以工作了,但它不适用于这个:/。 代码如下:

from random import randrange
numberboy == randrange(7,9)
if numberboy == "7":
    print ("You unluckily fall into a pit!")
    health -= 1
    if health == "1":
        print ("You drop to your knees and lie, filled with pain in the pit. You drop to the floor. Your quest is over.")
        print ("Your health has fallen to " + str(health) + ". ")



if numberboy == "8" or "9":
    print ("You could have fallen into a pit but you luckily didn't!")
    print ("You find a path leading of to another room.")

print ("You walk down the path and find a light illuminating an elvish sword!")
print ("You walk out of an escape path and find youself coming out of a secret entrance at the clearing.")
import time

顺便说一句,之前使用 'numberboy = 0' 以使其工作(numberboy 是变量,x)

【问题讨论】:

    标签: python random


    【解决方案1】:

    您正在将数字与字符串进行比较...

    用途:

    if numberboy == 7:
    

    代替:

    if numberboy == "7":
    

    健康也一样,用这个比较:

    if health == 1:
    

    另外,这是错误的:

    if numberboy == "8" or "9":
    

    改用这个:

    if numberboy == 8 or numberboy == 9:
    

    希望对你有帮助

    【讨论】:

    • 是的,它更好,但我认为它会混淆 OP
    • 谢谢!不敢相信我得到答案的速度有多快!
    【解决方案2】:

    我注意到的几件事:

    1. numberboy == randrange(7,9) 应该是 numberboy = randrange(7,9)
    2. 你永远不会用一个值来定义健康,你只会尝试从中减去。这将引发NameError
    3. 您正在将整数与字符串进行比较。应该是 numerboy == 7 而不是 numberboy == '7'

    【讨论】:

      【解决方案3】:
      numberboy == randrange(7,9)
      

      是你的问题。换成

      numberboy = randrange(7,9)
      

      randrange(x,y) 会给你这种号码x <= nb < y 所以它不可能是y

      【讨论】:

      • 谢谢!我尝试了所有其他解决方案,但仍然无法正常工作。底部的那条魔法线固定了它!
      【解决方案4】:

      您需要对整数使用 random.randint(floor,ceil),并且 random.random(floor,ceil) 用于小数

      【讨论】:

        【解决方案5】:

        所以有几点注意事项:请在代码中查看我的 cmets。 你需要声明健康,你有 numberboy 评估不等于。这两件事是您的代码中最大的问题。我的建议是下载pycharm 并学习如何更好地调试您的代码。有了更多的经验,这些错误就不会犯了。

        from random import randrange
        import time #this is unused
        
        
        numberboy = randrange(7,9)  # you had this evaluating not declared
        print numberboy
        health = 10  #you did not declare health 
        if numberboy == 7:  # numberboy is a int type no need for making it a string
            print ("You unluckily fall into a pit!")
            health -= 1
            if health == 1:
                print ("You drop to your knees and lie, filled with pain in the pit. You drop to the floor. Your quest is over.")
                print ("Your health has fallen to " + str(health) + ". ")
        
        if numberboy == 8 or 9:
            print ("You could have fallen into a pit but you luckily didn't!")
            print ("You find a path leading of to another room.")
        
        print ("You walk down the path and find a light illuminating an elvish sword!")
        print ("You walk out of an escape path and find youself coming out of a secret entrance at the clearing.")
        

        【讨论】:

          猜你喜欢
          • 2021-12-10
          • 2021-02-07
          • 2013-02-17
          • 1970-01-01
          • 2010-10-30
          • 1970-01-01
          • 2016-02-16
          • 2017-03-03
          • 2013-12-25
          相关资源
          最近更新 更多