0

first of all, hello to everyone! I'm a first timer in here! anyways, I had this problem and I dont have a real direction... I tried this:

def MyStars(inputList):
l = len(inputList)
ret = []
for x in inputList:
  ret.append("* ")
print(ret)

but then I realised that the output is a string of * which last the number of integers I have in the original List... the outcome is:

['* ', '* ', '* ']

while I want it to be for the list [3,9,7] for example:

*** ********* *******

can someone help? tnx

  • Given the integer `3` how could you make a string `***` ? There's lots of ways you could do it, you can almost do it with the skills you have used in your question; but you won't be able to solve the whole thing until you can do that part on its own. Then when you have that bit you can wrap it into doing one for each number. – TessellatingHeckler Aug 19 '22 at 15:55

3 Answers3

0

I would not use append but a list comprehension with string multiplication:

def MyStars(inputList):
    print(' '.join(['*'* i for i in inputList]))
    
MyStars([3, 9, 7])

output: *** ********* *******

To fix your code, you would need 2 loops, one to iterate over the numbers, and one to append your characters, then join your lists:

def MyStars(inputList):
    l = len(inputList)
    for x in inputList:
        ret = []
        for i in range(x):
            ret.append('*')
        print(''.join(ret), end=' ')
    
MyStars([3, 9, 7])

NB. note that this version gives a trailing space.

mozway
  • 194,879
  • 13
  • 39
  • 75
  • thanks m8! it worked perfectly ! do you mind explaining what you did in there ? – Ethan Yaacobov Aug 19 '22 at 15:57
  • I suggest you have a look, try to get it by yourself, then ask me questions about the part you don't understand. It might be a better way to understand ;) – mozway Aug 19 '22 at 16:00
  • "*List comprehension*" - terminology nitpick: [generator expression](https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions) – TessellatingHeckler Aug 19 '22 at 16:02
  • 1
    @TessellatingHeckler Opps, forgot the square brackets (yes, `join` is faster with a list comprehension than a generator) ;) – mozway Aug 19 '22 at 16:03
0

try this code.

What happened with your code is that you were close to the answer, but you needed to use the range function and loop through x to add the correct number of stars to your list.

def MyStars(inputList):
   l = len(inputList)
   ret = []
   for x in inputList:
       for i in range(x):
          ret.append("*")
       ret.append(" ")
   for a in ret:
       print(a)
0

If to print is all you want to do, you can just

print(*("*" * n for n in input_list))

which for input_list = [3, 9, 7] prints *** ********* *******

  • <s: str> * <n: int> multiplies string s n times e.g. "xYZ" * 3 will result in "xYZxYZxYZ" (in this case "*" * 3 will be "***")
  • ("*" * n for n in input_list) creates a collection of "*" * n-s for each n in input_list (this creates a generator that can be iterated only once, if you want to iterate the collection more than once, you can create e.g. a list([...]) instead)
  • print(*<iterable>) prints each item of the iterable and separates them with sep (that is by default a space, which happens to be what you want, you can pass sep as a print param, e.g. print(*<iterable>, sep="\n")

An example of fixing your solution (I also adjusted naming to follow python conventions):

def my_stars(input_list):
    stars_strings = []
    for x in input_list:
        stars_strings.append("*" * x)
    ret = " ".join(stars_strings)
    print(ret)
honorius
  • 481
  • 4
  • 7