Ask coding questions

← Back to all posts
How do I make the name a random one of the names? How do I make it where the name is not used again
ColePete (24)

How do I make the monster name a random one of my names in the monster_names section? And how do I make it to where that name is not chosen again?

Answered by Whippingdot (158) [earned 5 cycles]
View Answer
Commentshotnewtop
Whippingdot (158)

First, change monster names from {} surrounding it to [] surrounding it.
Then you have to add this:

import random

and this in the while loop before the if bow or sword:

monster_names2 = monster_names
random_name = random.randint(0, 4)
del monster_names2[random_name]
random_name = monster_names[random_name]
monster_names = monster_names2

and you have to replace:

monster = {"attack": 16, "health": 100}

in both places with:

monster = {"attack" : 16, "health" : 100, "name" : random_name}
ColePete (24)

@Whippingdot WOW thanks. (also, what do cycles do?)

ColePete (24)

@Whippingdot this is what my code looks like, but I keep getting an error message. Did i do something wrong?

ColePete (24)

@Whippingdot then why is there an error?

Whippingdot (158)

No no no, one sec. That was for you wow thanks comment. @ColePete

ColePete (24)

@Whippingdot oh, my bad. there is a bad delay on the comments.

Whippingdot (158)

Ok, create monster_names2. Then:

monster_names2 = monster_names
random_name = random.randint(0, 5)
del monster_names2[random_name]
random_name = monster_names[random_name]
monster_names = monster_names2

@ColePete

Whippingdot (158)

What my most resent answer meant is you create a second list with the same things as your original list. Then you get the random number, and you delete the name associated with the random number from the second list, then you get the name associated with the random number from the original list, and then you say the original list is now equal to the second list from which the name used was deleted. @ColePete

Whippingdot (158)

HEHEHE. my bad. 😢😢😢. The max number should be 4. UGH MEEEEEE!!!!.

random_name = random.randint(0,4)

@ColePete

ColePete (24)

@Whippingdot ok that worked THX! U cool if I reach out to u with any more questions?

Whippingdot (158)

Yes, I am cool. When you create a post, ping me using @Whippingdot k.

One sec

Oh, and my answer is exactly what you need for your code, so if you cannot change the answer from the other guy to me, try to at least upvote my replies. Thanks a lot for listening. I think you are the first person for me to have given a proper answer to. Bye! @ColePete

ColePete (24)

for those of you who are looking at this comment, please look lower at the upvoted comments, because there were some errors with this answer that were fixed down below.

Whippingdot (158)

Umm @ColePete, I fixed the main post too. They can just look at the main post. I have edited that.

MocaCDeveloper (523)

use the random module.

Example:

import random

names = ['bob','jeff','bobby','jeffy']
name = random.choice(names)
last_name = []
last_name.append(name)

# now, use this if statement to make sure the current name is not the last name
if name == last_name[len(last_name)-1):
   print("do some stuff here to update the new name")

But yeah, for a random name use the random module. And to make sure the random name is not the same as the last one, i'd probably make a list and append each new name to the list :D

MocaCDeveloper (523)

@ColePete

anytime! if you have anymore questions don't hesitate to ask!!

Whippingdot (158)

NOOOOO!! you were just faster than me. @MocaCDeveloper

MocaCDeveloper (523)

@Whippingdot

and my way is probably easier
Lol that's how I feel when coder100 answers every question before me :(

Whippingdot (158)

Hey, look below this, your way is the same as mine. You just didn't delete the item in the list. I also gave the exact directions based on his game
HOW DID HE GET 700 CYCLES IN 7 DAYS. Coder100 does his grinding. 100 cycles per day. @MocaCDeveloper

realTronsi (840)

@MocaCDeveloper hi. I'm in bed waiting to sleep while stalking forums lol, anyways I'll ping you tmr when I'm awake and ready

MocaCDeveloper (523)

@ColePete

nothing. Sometimes I like getting them though because it makes me feel like I accomplished something lol.

MocaCDeveloper (523)

@Whippingdot

ye i know it is absolutely insane

realTronsi (840)

@MocaCDeveloper if u delete the current comments and explain how the syntax treee thing works I could come back tmr morning and start working on it if I can understand it. (answer the questions I had in our discord dms)

Whippingdot (158)

I have no idea what you are saying. @realTronsi

Whippingdot (158)

Cycles do nothing boy. It just makes you seem more experienced. @ColePete