I need to write a program that will add a number of random letters after each letter in the supplied word.
Here is what I have:
import random
import string
def add_letters(word,number):
for i in range(number):
letters=''.join(random.choice(string.ascii_letters))
new_word=(letters.join(word))
return new_word
Here is what the output is supposed to look like if we enter add_letters(cat,1):
cZaQtR
My program always returns 1 letter even if I give it a different number. Also, it's always the same "random" letter after each letter in the word.
Any idea what I'm doing wrong?
Your logic is not correct. I would suggest adding some debugging printout inside your loop and see what you get, something like the following:
for i in range(number):
letters=''.join(random.choice(string.ascii_letters))
new_word=(letters.join(word))
print i, letters, word, new_word
This should tell you where you are going wrong. As an extra hint, you might use a second loop outside the first one.
There are a lot of logic errors here, and some clumsy coding (but don't worry, you'll get better while practicing)
new_word
gets erased ''.join
A correct answer would be something like:
def add_letters(word,number):
new_word = ""
for c in word: # iterate through the word
letters = ""
for k in range(number): # concatenate n random letters
letters += random.choice(string.ascii_letters)
new_word += c + letters # add the current char plus the letters to the new word
return new_word
You're never maintaining your added letters on each iteration.
You do new_word=(letters.join(word))
on every iteration, which will never keep the random letters from previous iterations, and using 'something'.join('another something') will add the 'something' in between every character of the 'another something'.
What you would want to look at instead, would be something more along the lines of
def add_letters(word, number):
new_word = ''
for character in word:
new_word += character
for _ in range(number):
new_word += random.choice(string.ascii_letters)
return new_word
which will iterate over every character of the supplied word, add it to the new_word
string, then randomly choose as many characters as required and add them to the new_word
, and continue till the original word is finished, and returns the new word.
Your problem is happening when you enter your loop you are always only generating at most one random character at a time with this:
letters=''.join(random.choice(string.ascii_letters))
You might want to look at your problem a different way, and instead, make use of another method in random called sample
With sample, you can provide how many random characters you want every time you call it, and they will be unique. This in effect changes your loop to instead of iterating over your number
, to iterate over the word and add your characters accordingly.
import random
import string
def add_letters(word,number):
new_word = ""
for i in word:
letters = ''.join(random.sample(string.ascii_letters, number))
new_word += "{}{}".format(i, letters)
return new_word
Calling:
add_letters('cat', 2)
Outputs:
cNbarHtTM
import random
import string
def add_letters(word,number):
"""
Adds "number" randomly selected ASCII letters to the end of "word"
"""
return word + "".join([random.choice(string.ascii_letters) for k in range(number)])
for i in range(10):
print(add_letters("foo", i))
The [random.choice...]
thing is a list comprehension. They're a really cool, powerful thing in Python, which I'd recommend you read up on if you don't fully understand the code.
As Conor said, the logic is off. Try and break apart the problem statement and see if you can find a solution to each. "add a number of random letters" - can you create this string?
after "each letter in the supplied word." can you split apart the word into separate letters?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.