dimanche 1 octobre 2023

Why does the range element in my code have problems when is uses the last number?

I am using the following code, which just selects a random number from a range and uses it as chance for other elements to be selected, and it works fine with no errors, but occasionally the number that it selects from the range(usually the last number like 16 or 20) does not work, and I don't know why.

Code:

import random

# List used for Age Classifications
ageClass = ["Newborn", "Young", "Aged", "Old", "Ancient", "Timeless"]
randAgeClass = ""
randAge = 0

# Probability of age classes
# Newer classes are much more likely to be selected than the older classes
chance = random.randrange(1, 20)
# Chance Debug
print(chance)

if chance in range(1, 5):
    randAgeClass = "Newborn"
elif chance in range(6, 10):
    randAgeClass = "Young"
elif chance in range(11, 13):
    randAgeClass = "Aged"
elif chance in range(14, 16):
    randAgeClass = "Old"
elif chance in range(17, 18):
    randAgeClass = "Ancient"
elif chance in range(19, 20):
    randAgeClass = "Timeless"
else:
    print("error")

# If statements for age classes
if randAgeClass == "Newborn":
    randAge = random.randrange(1, 20)
elif randAgeClass == "Young":
    randAge = random.randrange(21, 80)
elif randAgeClass == "Aged":
    randAge = random.randrange(81, 250)
elif randAgeClass == "Old":
    randAge = random.randrange(251, 500)
elif randAgeClass == "Ancient":
    randAge = random.randrange(501, 1000)
elif randAgeClass == "Timeless":
    randAge = random.randrange(1001, 6000)
else:
    print("error")

print("Vampire Name: Steve")
print("Age Classification:", randAgeClass)
print("Age:", randAge)

Here is how a successful output looks like: enter image description here

And here is how an unsuccessful output looks like: enter image description here

This only seems to be happening if the random number in the range that is chosen is the last number in the range like 5, 10, 16... Why is this happening and how do I fix this?

I tried using a list and that didn't work, so I would rather just use a range for this.




Aucun commentaire:

Enregistrer un commentaire