Python Forum
Unknown Error with Random Stat Assigner
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unknown Error with Random Stat Assigner
#1
So, essentially, I am trying to make a simple program in python 3 (using CodeHS, if that matters) for a theoretical RPG concept. Basically, it is supposed to take the level increase (which can be decided randomly or by user input) and assign a stat point to a random stat, where the theoretical character gets 1 stat point per level. No matter what I do, the number of levels is inconsistent with the stat values, so a "character" who levels up 43 times might only get 41 stat points. The system that generates stats is in a for loop using the level value as the repeated amount of times. I wish I could provide more information, but I am unaware of what is causing this issue or how to recreate it. I have tried creating multiple "repair sequences". Communication is not my strong point, so I will provide the code (with comments on what each segment does) in all of its glory.

import random

# This area provides instructions for the user.
print("Welcome to the Spellcrest Beta Stat Assigner.")
print("infer your character or enemy is at level 1, and")
print("choose how many levels you want to add. Or leave it random!")
print("the minimum level gain is 1, and the max is 100. No decimals.")
print("starting from other levels is planned... some day.")
print("")
print("Choose Level? (1 for yes, 2 for random level within range.") 

# User Input determines how the amount of points are chosen.
randomcheck = int(input("Anything else will randomize it between all possible values [1-100]): "))

# Choosing "1" will let the user select the level.
if randomcheck == 1:
    level = int(input("How many levels are gained?: "))
    print("")
    print("level gain: ")
    print(level)
    # Error messages prevent going out of intended range.
    if level > 100:
        print("Level cannot be over 100. Please try a number between 1 and 100.")
        quit()
    if level == 0:
        print("Why are you here if you aren't gaining any levels? (Please try a number between 1 and 100.)")
        quit()
    if level < 0:
        print("Even if losing levels was possible, this program assumes the character is gaining points") 
        print("from level 1.")
        print("Please try a number between 1 and 100... a positve one.")
        quit()

"""
Choosing 2 will ask the user to input a level range, and randomly select a
number within that range.
"""
if randomcheck == 2:
    levellow = int(input("Lowest Possible Level Gain: "))
    levelhigh = int(input("Highest Possible Level Gain: "))
    
"""
it was mandatory that I made this error code before the level value was assigned,
since it would have triggered an error.
"""
if levellow > levelhigh:
    print("")
    print("People like you are why I have to make these error messages.")
    print("Lowest possible level must be lower than highest possible level gain")
    print("they can be equal, but you'd be better off using 1 at the start of the program.")
    quit()

"""
inferring that everything goes well, this will generate a random level within
the range of the numbers the user input.
"""

level = random.randint(levellow, levelhigh)

"""
here I put various easter eggs and error messages. Speaking of easter eggs,
how did you get in here? (This doesn't apply to forum users reading a thread
I may or may not have made in the event I can't figure out my calculation error
that may or may not still be present)
"""

if levellow <= 0:
    print("")
    print("Invalid lowest possible level gain.")

if level <= 0:
    print("Your value was", level, ", so the program will end.")
    quit()

if level > 0:
    print("...However, your value was", level, ", so I guess I'll let it slide.")
    
if levelhigh > 100:
    print("")
    print("Invalid highest possible level gain.")
        
if level >= 100:
    print("Your value was", level, ", so the program will end.")
    quit()
        
    if level < 100:
        print("...However, your value was", level, ", so I guess I'll let it slide.")
    
    if levellow == levelhigh:
        print("You goof, why didn't you just pick 1 at the beginning?")

# Tells the user the randomly selected level gain.    
    print("")
    print("level gain: ")
    print(level)

# Conditional easter egg.
    if levellow == levelhigh:
        print("big surprise.")
    
    
"""
If the user chooses anything else, the program will randomly select a number.
"""

if randomcheck > 2 or randomcheck <= 0:
    level = random.randint(1, 100)
    print("")
    print("level gain: ")
    print(level)
    
"""
defining stat values.
"""
HP = 0
MP = 0
ME = 0
ATK = 0
SPD = 0

"""
This little system here will randomly generate a value and assign a point to
a stat determined by that value. I felt really smart for coming up with this
but I'm likely not the first or the last.
"""

print("")
print("assigning stats...")
print("") 
for i in range (level):
    statpoint = random.randint(1, 100)
    if statpoint in range(1, 20):
        HP = HP + 1
    if statpoint in range(21, 40):
        MP = MP + 1
    if statpoint in range(41, 60):
        ME = ME + 1
    if statpoint in range(61, 80):
        ATK = ATK + 1
    if statpoint in range(81, 100):
        SPD = SPD + 1
        
"""
This "total" variable is the total points assigned, used to check if the level
and total points assigned are consistent.
"""
total = HP + MP + ME + ATK + SPD
print("Complete!")

"""
Notifies the user what points were assigned to what stats.
"""

print("")
print("Health increased by", HP)
print("Magic Power increased by", MP)
print("Magic Energy increased by", ME)
print("Attack increased by", ATK)
print("Speed increased by", SPD)
print("")

"""
Notifies the user of the comparison between the level and total points.
Ideally, this error message shouldn't appear.
"""
print("this makes a total of", total, "assigned points for a level gain of", level)
if total != level:
    print("Inconsistent Results. (Level gain not equal to Total Assigned Points.)")
I am new to the forums so i apologize if my formatting was incorrect. As for operating system, I have tried this on both ChromeOS and Windows 10. My general question is, how can I make the two output values match consistently?
"I want to be a human being, not a human doing."
Reply
#2
I forgot to mention: As I do not know the cause or nature of this glitch, I was unable to put it in a more specific category. My apologies.
"I want to be a human being, not a human doing."
Reply
#3
randint(1, 100) generates random numbers in a range starting with 1 and ending with 100.
range(1, 100) starts at 1 but ends at 99.

You don't bump up any attribute if statpoint == 100.
FC8 likes this post
Reply
#4
(Dec-01-2021, 06:07 PM)deanhystad Wrote: randint(1, 100) generates random numbers in a range starting with 1 and ending with 100.
range(1, 100) starts at 1 but ends at 99.

You don't bump up any attribute if statpoint == 100.

Thank you, so what should I do? I kind of just learned python through guides and stuff. I changed
if statpoint in range(81, 100):
        SPD = SPD + 1
to
if statpoint in range(81, 101):
        SPD = SPD + 1
and tried changing
if statpoint in range(1, 20):
        HP = HP + 1
to
if statpoint in range(0, 20):
        HP = HP + 1
but changed it back.
"I want to be a human being, not a human doing."
Reply
#5
I changed
if statpoint in range(81, 100):
to
if statpoint in range(81, 101):
but I'm guessing that's not what I was supposed to do.

edit: Sorry I said the same thing twice. It took a bit for the edit to load so I assumed it didn't work.
"I want to be a human being, not a human doing."
Reply
#6
You need the range of the generator to match the range of the checkers. Since Python starts at 0 I would generate values 0..99 to have 100 possible values for statpoint. Next I would write the if code differently
for i in range (level):
    statpoint = random.randint(0, 99)
    if statpoint < 20):
        HP = HP + 1
    elif statpoint < 40:
        MP = MP + 1
    elif statpoint < 60:
        ME = ME + 1
    elif statpoint < 80:
        ATK = ATK + 1
    else
        SPD = SPD + 1
Your RPG is going to be pretty lonely with only 1 player and no NPC's. How do you keep track of stats for multiple characters?
Reply
#7
(Dec-01-2021, 07:38 PM)deanhystad Wrote: You need the range of the generator to match the range of the checkers. Since Python starts at 0 I would generate values 0..99 to have 100 possible values for statpoint. Next I would write the if code differently
for i in range (level):
    statpoint = random.randint(0, 99)
    if statpoint < 20):
        HP = HP + 1
    elif statpoint < 40:
        MP = MP + 1
    elif statpoint < 60:
        ME = ME + 1
    elif statpoint < 80:
        ATK = ATK + 1
    else
        SPD = SPD + 1
Your RPG is going to be pretty lonely with only 1 player and no NPC's. How do you keep track of stats for multiple characters?

I could have sworn I replied to this. This is not directly relating to the RPG itself, but is instead a concept for an algorithm for generating enemy levels. Let's say you are playing a tabletop RPG with your friends (which are not my area of expertise so forgive me if this is a bad example), and their characters encounter a goblin. Now let's say I am the gamemaster and being the creative drain I am, I cannot decide the stat levels for the monster. (The characters, or players, choose their stats or level them up. An RPG with random player stats would be frustrating, if I may say so myself.) Anyway, I can choose the level of the goblin by inputting a range (in RPGs, levels tend to pertain to areas.), leave it random, or decide myself. Then it would assign the stats for me so I don't have to.

If I were to apply this to a game, each stat point would give a certain amount of stats pertaining to the monster... please forgive my terrible communication skills. If you've ever played ARK: Survival Evolved, tamed creatures can be assigned points upon leveling up, which increases the stats by a value that depends on the creature and/or chosen stat. This would automatically apply this to monsters, with adjustments, of course.

And of course, thank you very much! Big Grin
"I want to be a human being, not a human doing."
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Error UserWarning: Unknown option ssl_ca_certs warnings.warn(str(exc)) Vijayaraj 0 734 Jul-11-2023, 06:01 AM
Last Post: Vijayaraj
  Failing to get Stat for a Directory tester_V 11 3,588 Jul-20-2021, 10:59 PM
Last Post: Larz60+
  Unknown error occurred: Port not found NewBeie 0 1,451 Aug-27-2020, 08:50 PM
Last Post: NewBeie
  Help!Unknown ERROR bwdu 1 2,065 Apr-20-2020, 02:09 PM
Last Post: deanhystad
  Unknown error TheIDarKIKnight 0 1,464 Apr-19-2020, 05:27 PM
Last Post: TheIDarKIKnight
  import error (unknown location) pseudo 1 10,799 Nov-14-2019, 11:47 PM
Last Post: pseudo
  Unknown error in pygame :( TheDovah77 1 2,886 Apr-14-2019, 10:22 PM
Last Post: metulburr
  Unknown syntax error (Im new to this) reasonablelevel 2 2,844 Jul-25-2018, 11:59 AM
Last Post: reasonablelevel
  weird error in random sentence generator bobger 9 5,727 Nov-29-2017, 07:34 PM
Last Post: bobger
  Unknown Syntax Error AlwaysNew 6 5,281 Sep-09-2017, 01:57 AM
Last Post: AlwaysNew

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020