#How do I make python generate a random number of random strings from a list?

88 messages · Page 1 of 1 (latest)

queen forum
#

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.

chilly nimbus
#

What do you mean a "random number of random strings"

#

Is the output not what you wanted?

queen forum
#

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.

chilly nimbus
#

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

sterile radishBOT
#
Code Formatting

When sharing code with the community, please use the correct formatting for ease of readability.

Example

```py
YOUR CODE HERE
```

Those are back ticks not single quotes, typically the key above TAB

queen forum
#

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)))```
chilly nimbus
#

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?

queen forum
#

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

chilly nimbus
#

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?

queen forum
#

I don't know, I've even pulled out my old python class notes and couldn't find anything helpful for this.

chilly nimbus
#

That's weird

#

It's one of the first things you learn

queen forum
#

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.

chilly nimbus
#

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 ....?

queen forum
#

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.

chilly nimbus
#

IDLE is goated tho lol

queen forum
#

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.

chilly nimbus
#

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

chilly nimbus
chilly nimbus
#

!exec ```py
Spellbook1 = ["resgen ","create water ","teleport ","light ","sting "]
for word in Spellbook1:
print(word)

sterile radishBOT
chilly nimbus
#

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!")

sterile radishBOT
chilly nimbus
#

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?

queen forum
#

I'm really sorta lost I apologize

chilly nimbus
#

No worries

queen forum
#

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.

chilly nimbus
#

Yup, that's probably a good next step

queen forum
chilly nimbus
#

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

queen forum
#

I am lost again I'm sorry

chilly nimbus
#

It's okay

#

Take your time

#

Or keep asking questions

queen forum
#

also, please note I am using a text reader for this conversation.

chilly nimbus
#

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

queen forum
#
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?

chilly nimbus
#

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

queen forum
#

I generally learn by picking apart existing code, so I can refrence syntax with somethin I know works.

#

Thank you.

chilly nimbus
#

But you are close

#

Also don't forget to close your print function on the last line

whole pond
#

wang is pro at python

chilly nimbus
#

Not at all. I have plently more to learn

queen forum
chilly nimbus
#

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

queen forum
#

oh i see

whole pond
#

Hopefully I pick up python pretty quick next semester, I’m gonna take Python class in college

queen forum
#

Also when I decide I don't want duplicates, but for now I can just manually get rid of those.

chilly nimbus
#

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

queen forum
#

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.

chilly nimbus
#

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)