Currently, I have this, it gives me a random number of strings, but it's all the same thing.
This is not a homework question, I'm trying to make a program that can generate spellbooks & catalogs for a super homebrew d&d campaign. (I plan to make this more complex later on.)
The last time I used python, I was in 9th grade, I'm very rusty.
#How do I make python generate a random number of random strings from a list?
88 messages · Page 1 of 1 (latest)
What do you mean a "random number of random strings"
Is the output not what you wanted?
It's almost there, I want each string it gives me to be different, I don't mind duplicates I just don't want them all to be the same thing.
ex a player finds a pamphlet of a few spells on it, I want to press a button, and it gives me a list of a few spells that would be on the thing.
Oh, I think I know what you mean
Also, please when sharing code use either code blocks or some sort of code host
Otherwise others can't copy and paste your code
!format
oh i know how to do that it's just been a bit since I've asked for code help on discord
Spellbook1 = ["resgen ","create water ","teleport ","light ","sting "]
print (random.choice(Spellbook1)*(random.randint(1,5)))```
random.choice(Spellbook1)*(random.randint(1,5)) This as you clearly can tell, does not run the random choice multiple times
For that you want some sort of loop
The easiest being a for loop
That way it finds a random number say n, then it does random.choice(Spellbook1) once, then twice, etc, until you have chosen n number of words randomly
Does that make sense?
I don't follow, I tend to learn this stuff better by seeing an example of functional code doing what I'm looking for
I'm very bad with the syntax
I prefer not giving code directly unless it's absolutely necessary. People actually learn better by trying to implement things themselves and tinkering around
I can walk you through the thought process though
Do you know what a for loop is and how it works?
I don't know, I've even pulled out my old python class notes and couldn't find anything helpful for this.
to be fair, I am an incredibly shitty note-taker. At the time, I just remembered it all, but it's been like 4 years since then.
Doesn't matter. Pretty much it looks like this ```py
for some_var in some_iterator:
do stuff
Does that ring a bell?
for ... in ....?
Nope
The class I had was taught by a really old dude who forced us all to use IDLE, my dad even said the way that guy taught was bonkers when I had him help with my hw.
IDLE is goated tho lol
My dad learned in pycharm, and I started using it for my hw assignments
I'm on chromeOS rn though so I can't use it.
Okay anyways, loops are used to repeat a block of code to do things over and over again. For example that block could be to randomly choose a word from your list. We want to do that over and over again (to get multiple words) so it makes sense to use a loop
It doesn't matter what editor one uses as long as they can do what they need with it
For example, we can loop over a list like this and we can use the values of each round in a variable
!exec ```py
Spellbook1 = ["resgen ","create water ","teleport ","light ","sting "]
for word in Spellbook1:
print(word)
@chilly nimbus
✅ Exec - Success (Super User 🦸)
resgen
create water
teleport
light
sting
!exec modules | Completed in 0.0294 milliseconds
You see how the loop went over the list one by one, and we can do things with each item like print it out
Now, we don't have to just iterate over a list. A handy thing we can use when we want to iterate a certain number of times is use the range function
For example
!exec ```py
for i in range(5):
print(f"Iteration {i}: We are doing stuff!")
@chilly nimbus
✅ Exec - Success (Super User 🦸)
Iteration 0: We are doing stuff!
Iteration 1: We are doing stuff!
Iteration 2: We are doing stuff!
Iteration 3: We are doing stuff!
Iteration 4: We are doing stuff!
!exec modules | Completed in 0.0386 milliseconds
Of course, 5 can be replaced with a different integer value
How do you think we can use range and a for loop to achieve what you're trying to do?
I'm really sorta lost I apologize
No worries
Thank you for telling me that I need to figure out how to a loop, I know what to look for a tutorial on now.
Yup, that's probably a good next step
this is probably a stupid question, what does the i mean?
It's not a stupid question don't worry
When you do a for loop, it goes through each item in whatever comes after in. These item's values can be saved in a temporary variable (which I named i) so that you can use the current item inside the body of the loop
for i in range(5):
print(f"Iteration {i}: We are doing stuff!")
``` So for this example, we're looping over `range(5)` which you can think of as a collection of `(0, 1, 2, 3, 4)`
Each iteration of the loop grabs one item and stores the value in the variable in between the for and the in
Like the first loop iteration will grab the 0, then makes i's value equal to 0, which is why when inside the loop I printed f"Iteration {i}: We are doing stuff!" out, the value of i was 0
And so on
I am lost again I'm sorry
so, what is the i variable doing here?
also, please note I am using a text reader for this conversation.
It's there to grab the value of whatever item the loop is currently on so that I can use it inside the loop. Note that it doesn't need to be named i as it is just a variable name
Spellbook1 = ["resgen ","create water ","teleport ","light ","sting "]
for i in range((random.randint(1,5)))
print (random.choice(Spellbook1)```
got an error,
how can I make it loop a random number of times?
That's close! The logic is there, you just have to make sure your syntax is correct
Make sure that after your for loop line there's a colon, and that any lines of code that you want repeated in the loop are indented
I generally learn by picking apart existing code, so I can refrence syntax with somethin I know works.
Thank you.
You can refer back to the examples I've given above as well
But you are close
Also don't forget to close your print function on the last line
wang is pro at python
Not at all. I have plently more to learn
What syntax do I use for this?
A closing parenthesis?
Look at the number of open parenthesis and the number of closing ones
You have 2 open 1 close which means the print function will throw an error
oh i see
Hopefully I pick up python pretty quick next semester, I’m gonna take Python class in college
It works now, thank you. I'll probably come back once I run into issues with the next iteration of this, which I want to pull from multiple lists.
Also when I decide I don't want duplicates, but for now I can just manually get rid of those.
Good luck! Just one last thing. A for loop achieves what you want to do, and it was good that you got to learn about them, but you should also be aware that the random library also has random.choices and random.samples as well
However, I don't think you should worry about those at the moment. You should just know that those features also exist and that you can potentially use them in the future
I was vaguely aware of those, but don't exactly know how they differ from the one I'm using/what they're used for generally.
random.choice choses 1 value. random.choices can choose multiple values with the items being replaced (i.e. duplicates allowed), and random.sample can choose multiple values without replacement (i.e. no duplicates chosen)