简体   繁体   中英

Python - why is my random number generator not working?

I've tried for ages on how to get this to work. It is supposed to choose a random number with 'from random import randrange' followed by the line 'x = randrange(1,3). I have used that generator at other times and it worked then but it will not work with this :/. Here is the code:

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

Btw, earlier on a use 'numberboy = 0' in order for it to work (numberboy is the variable, x)

You are comparing a number to a string...

Use:

if numberboy == 7:

instead of:

if numberboy == "7":

The same with health, use this comparison:

if health == 1:

Also, this is wrong:

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

Use this instead:

if numberboy == 8 or numberboy == 9:

Hope this helps

Couple of things I noticed:

  1. numberboy == randrange(7,9) should be numberboy = randrange(7,9)
  2. You never define health with a value, you only try to subtract from it. That will throw a NameError
  3. You are comparing integers to strings. Should be numerboy == 7 instead of numberboy == '7'
numberboy == randrange(7,9)

is your problem. Just replace it by

numberboy = randrange(7,9)

and randrange(x,y) will give you this kind of number x <= nb < y so it can't be y

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

So a few notes: please see my comments in the code. you need to declare health, you have numberboy evaluating not equating. those two things are the biggest problems in your code. my advice is to download pycharm and learn how to debug your code better. With more experience these mistakes would not have been made.

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.")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM