1

I'm trying to make a name generator:

from random import randint
relation = {
    'A' : ['B', 'C', 'D', 'F', 'R', 'Y'],
    'B' : ['E', 'O', 'I'],
    'C' : ['A', 'E', 'H', 'R', 'O', 'I'],
    'D' : ['A', 'E', 'H', 'R', 'O', 'I'],
    'E' : ['R', 'T', 'P', 'S', 'F', 'L', 'X'],
    'F' : ['E', 'U', 'I', 'O', 'A'],
    'G' : ['R', 'O', 'A'],
    'H' : ['E', 'I', 'O', 'A'],
    'I' : ['N', 'X', 'S', 'E', 'T', 'P', 'L', 'M'],
    'J' : ['A', 'I', 'O', 'Y'],
    'K' : ['I', 'E', 'A'],
    'L' : ['I', 'E'],
    'M' : ['O', 'Y', 'I'],
    'N' : ['E', 'I', 'O', 'A'],
    'O' : ['V', 'T', 'N'],
    'P' : ['I', 'A', 'E', 'O'],
    'Q' : ['U', 'E', 'I'],
    'R' : ['E', 'I', 'A'],
    'S' : ['T', 'I', 'O', 'A', 'H'],
    'T' : ['H', 'E', 'I'],
    'U' : ['B', 'G', 'L'],
    'V' : ['E', 'U', 'I', 'A'],
    'X' : ['I', 'O'],
    'Y' : ['E', 'L'],
    'Z' : ['O', 'I']
}
char = (raw_input("Enter an English alphabet: ")).upper()
letters = int(raw_input("How many letters: "))
for i in range(0, letters):
    if i==0:
        print char,
    else:
        print char.lower(),
    char = (relation[char])[randint(0, len(relation[char])-1)]
print ''
raw_input("Press [ENTER] to exit...")

But the problem is that there is a whitespace when it prints the name. For example:

Enter an English alphabet: T

How many letters: 5

T i p a y

Press [ENTER] to exit...

How to remove the whitespace?

P.S: I'm a beginner :)

Community
  • 1
  • 1
  • possible duplicate of [How to print a string of variables without spaces in Python (minimal coding!)](http://stackoverflow.com/questions/3249949/how-to-print-a-string-of-variables-without-spaces-in-python-minimal-coding) – user80551 May 06 '15 at 13:02

2 Answers2

3

It's the commas that's causing this. print statements separated with commas add white spaces:

print "a", "b"

Prints a b

print "a",

Prints (with white space)

print "a"

Prints a (without white space)

You can, however, change your code to use a variable:

name = ''
for i in range(0, letters):
    if i==0:
        name += char
    else:
        name += char.lower()
    char = (relation[char])[randint(0, len(relation[char])-1)]
print name
print ''

Or shorter and more efficient:

letter_list = []
for i in range(0, letters):
    letter_list.append(char)
    char = (relation[char])[randint(0, len(relation[char])-1)]
name = ''.join(letter_list)
print name.lower().capitalize()
print ''
Richard de Wit
  • 7,102
  • 7
  • 44
  • 54
  • 1
    But then it will print on different lines, which is not what he wants @GreenHenk – heinst May 06 '15 at 12:59
  • Oh! Well now I don't know whose answer I should accept. – StayAheadOfLife May 06 '15 at 13:08
  • 2
    @heinst and GeenHenk: FWIW, building a string char by char like that is pretty inefficient in Python, since strings are immutable. It's _much_ better to store the chars in a list and then use `.join` to build a string from the list. Of course, it probably doesn't matter much for this program, but it's a good habit to get into. And although the list-based approach is a little more complicated than `name += char` it looks like Coderboy91 isn't afraid to use lists in his code. :) – PM 2Ring May 06 '15 at 13:40
3

I am not exactly sure your reasoning for printing as you go along, but you could have a word variable and then append all the char you come up with to the word variable and just print it at the end. This will not have the spaces in between the letters

word = ''
for i in range(0, letters):
    word += char
    char = (relation[char])[randint(0, len(relation[char])-1)]
print word.lower().capitalize()
print ''

Based on PM 2 Rings suggestion you could also do it this way:

charList = []
for i in range(0, letters):
    charList.append(char)
    char = (relation[char])[randint(0, len(relation[char])-1)]
print ''.join(charList).lower().capitalize()
print ''
heinst
  • 8,520
  • 7
  • 41
  • 77