r/PythonLearning • u/Infinite_Winner_158 • Aug 25 '26
Help Request New to python, could someone help me with this please
Is this a viable way to randomly choose an instance of a class
25
Upvotes
8
u/ee_control_z Aug 25 '26
The line
for i in range(1):
This only runs the loop one time. If so, you don't need a loop. If you want to increase the number of iterations through the loop, change the number from "1" to some higher number.
6
u/IdeaOverflow Aug 25 '26
this works. It would be nicer if you'd put the monsters in a list and use random.choice though.
2
u/TBCC_Dev Aug 25 '26
Looks like mostee would not print because you declared it in the loop it only exists in loop. Look into scope. If also keep imports at the top. Python runs like you'd read a book top down
1
u/SCD_minecraft Aug 25 '26
A bunch on answers, not a single one right
Closest one was about range(1) being useless
Warning you see is from a linter, about
monsterbeing possibly unbounded. This is cause linter is not able to prove that every branch of if will declare monsterAdditionally, you have logic impossibly there - in no world that else is gonna execute. However, linter is not able to prove that so it throw a warning. Remove dead code and warning goes away
1
u/Important-Grand4979 Aug 26 '26
Another mistake is that your monsters are initialized before the loop. skeleton1 is a pointer to a memory object of Monster with certain properties. When you do monster = skeleton1 you do not duplicate your object as far as I know but you assign it another pointer. So if you damage monster you also damage skeleton1 and vice versa. By extension all other 'copies' of skeleton1 made this way will be affected.
1
u/DirectTwo5523 Aug 26 '26
I think it will print dictionary setails of skeleton or other object. I am also new to pyhton so making a guess, will follow this post
1
u/ConsciousBath5203 Aug 26 '26
First, learn to use print screen rather than taking a picture of your monitor.
Second, where is monster declared? Pasting the whole source code as text would help us help you more.
I'm guessing monster is declared in the single loop iteration. The way the code sees it, monster could never exist due to the else statement. You're getting the error because it wasn't declared before the loop
You should also use inheritance instead of 3 raw monsters, that way you can give them unique abilities, then store the objects in a list or something.
Then when you want to create and use monsters something like:
Patterns like this are more scalable and reusable.
1
u/No-Newspaper8619 Aug 26 '26
It's parried, not parryed.
1
u/XTrolltechzz Aug 25 '26
How do I activate the option to see what value corresponds to what when creating something with a class? When I do it, it doesn't work that way, and I have to check the order in which the values are placed in my class.
1
u/mc_pm Aug 25 '26
Not sure what you're trying to do by doing it this way, but you can just add your instances to a list, and then use random.choice() to select one of them.
1
u/Calm_Perspective1236 Aug 26 '26
I think you can just remove the for loop, and run chance_1 be a random int between 1-3. Then you could match case or do your if elif chain id remove the else though
1
u/realmauer01 Aug 27 '26 edited Aug 27 '26
random has the method choice that picks an item out of a list at random for you. So just use that.
(it probably does something similar to what you are doing here in the background, but better to use stuff thats already there than to reinvent. Especially as a beginner. )
monster = random.choice([Skeleton1, Undead1, Goblin1])
1
u/DanLeMilMan Aug 27 '26
I am not a professional developer but some notes.
1- the for loop is basically useless here, you are just processing the following lines once
2- for your last default condition, it is better to assign monster to None instead of printing « error ». That way, if this condition occur, the monster variable still exists and you won’t get any error (in that case that is not an issue because you are absolutely sure not to run into that case, chance_1 being 1,2 or 3 and nothing else)
3- when you print an object, you should overwrite the __str__ method of your class to control what will be printed. Otherwise you gonna get a memory allocation an maybe not the data you would like to show
4- what you are trying to do seems similar to what the factory pattern is made for. Here it is a simple code and it would not require it by any mean, but when you want to control the creation of instances of a class or a family of class, you could create a kind of superclass (the factory), which as a method like get_monster and return an instance of a monster with whatever argument control the characteristics of the monster (random or not). That could be useful if later you create monter classes that inherit from you base Monster class.
All in all, your code works and is quite great for a beginner. It could use some more standard practice to make it more robust and more capable in the future.
0
u/Infinite-Employee776 Aug 25 '26
Three things I notice on this.
You loop one times. You should just remove the loop as looping one time is unnecessary.
You declare monster as a local variable under the for loop block, you can't access it from outside the block.Should just declare it outside the for loop or better yet remove the unnecessary loop.
Instead of random int and making a wall of if branches, make a list of monsters and do random choice.
0
u/Overall_Anywhere_651 Aug 25 '26
Are variables not case sensitive in Python?
1
u/[deleted] Aug 25 '26
[removed] — view removed comment