Python Forum
Need help for my code!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help for my code!
#1
Hey guys, I have a problem and I'm really confused of how to do it. Anyone know this?

Problem: print 100 random strings(all lowercase letters), each increasing 10 characters in each loop. Start with 10 characters.

Eg:
xcdsfertdf
iuperdfghjufgqwersyt
tyuierdfhsjgkpoudfghrtdgckdvcn
.
.
.
.
.
100th loop

Can anyone help please??


I tried the code but failed to increment the string each time:
import random
import string

def random_word():
    random_words = ''.join(random.choice(string.ascii_lowercase)for i in range(10))
    while 0 > len(random_words)
    return(random_words)


for i in range(100):
    print(random_word())
Reply
#2
so what have you tried?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This is an easy problem.

Start by solving the problem of how do you generate a random string of length n. I would start by reading about the random library. It is filled with functions for generating random values and sequences of values.

Once you know how to generate the random strings write a loop that executes 100 times. Try generating 100 strings all length 10. Can you get that to work?

After the loop is working, figure out how to increment the length of the string for each iteration. You can use an equation that is based on the iteration (look at enumerate) or you could use a variable that you increment, or you could read up on the for loop.
Reply
#4
Hey,
I've done it till printing the strings of length 10 a hundred times. BUT I'm still am hung up on incrementing it each time it. What do I do? I'm hitting my head hard for this problem(even if it's an easy problem :( ).
Wall Think Huh
Reply
#5
All you need to do is count by 10. Counting by 10 is easy.
x = 10
x = 20
x = 30
x = 40
If I know the previous value of x how do I calculate the current value of x?

Remember to solve the problem first, then write the code.
Reply


Forum Jump:

User Panel Messages

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