1

I'm completely new to programming and to python. I'm using python 3.9 at the moment, trying to go through a simple python tutorial, but one of the tasks gives me problems.

Im supposed to get an output like this: (after asking the user up to which number he wants to go)

1*
2**
3***
4****
5*****
6******
7*******

My code looks like this at the moment:

usercount = int(input("Up to what number do you want to go?"))

for x in range(usercount):
    print(x+1, "*")

and the output looks like that:

1*
2*
3*
4*
5*
6*
7*

What do I have to do?

Thank you.

Blaupunkt
  • 117
  • 7
  • You have to add more stars. The Python way is `(x+1)*"*"`. – Thomas Sablik Oct 22 '20 at 07:44
  • Does this answer your question? [Multiplying a string with a number in python](https://stackoverflow.com/questions/3536996/multiplying-a-string-with-a-number-in-python) – mulaixi Oct 22 '20 at 07:47
  • Does this answer your question? [Display string multiple times](https://stackoverflow.com/questions/963161/display-string-multiple-times) – Georgy Oct 28 '20 at 19:49

9 Answers9

2

This would work, but lots of different ways of making it work:

usercount = int(input("Up to what number do you want to go?"))
stars = ''
for x in range(usercount):
    stars += '*'
    print((str(x+1))+stars)
E.J. Brennan
  • 45,870
  • 7
  • 88
  • 116
1

You can multiply a string by an integer to reproduce the string N times. For example:

IN: 'ab' * 3
OUT: 'ababab'

In your case, you want to multiply the string '*' based on the iteration number. Watch out, python range starts at 0 and ends at usercount-1.

Mathieu
  • 5,410
  • 6
  • 28
  • 55
1

use enumerate:

usercount = int(input("Up to what number do you want to go? "))

for i in range(usercount):
    print(str(i+1) + (i+1) * "*")

# output 
1*
2**
3***
4****
5*****
6******
7*******
Tasnuva Leeya
  • 2,515
  • 1
  • 13
  • 21
1

you are only printing * in print(x+1, "*"). You should multiply * with number of times you want to print it. Eg:

1* '*' = *
2* '*' = **

etc

Try,

usercount = int(input("Up to what number do you want to go?"))

for x in range(1,usercount+1):
    print(x, x*"*")
pjk
  • 547
  • 3
  • 14
1

Should another loop for *, two ways to go

  1. duplicate string by times a number.
  2. another inner loop to print how many *, but you have to specified option end to empty string for method print if no newline required for each print.
Jason Yang
  • 11,284
  • 2
  • 9
  • 23
1

You can do this:

In [1687]: usercount = int(input("Up to what number do you want to go?"))

In [1697]: for i in range(1, usercount + 1):
      ...:     print(str(i) + ("*" * i))
      ...: 
1*
2**
3***
4****
5*****
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
1

You have to print n start where n is the current number. To do something n times you would use a for loop.

In the example above you were using just a single print("*") which prints just a single start. To print more you would either print stars in inner loop, or create a single string containing stars:

for x in range(usercount):
    print(x+1, end="") # end="" means that print won't end in new line
    for i in range(x+1):
        print("*", end="")
    print() # just add a new line at the end

Or by creating a star string:

for x in range(usercount):
    stars = "*" * (x+1)
    print(x+1, stars)

Also to avoid typing x+1, you can specify the range to be range(1, usercount+1)

Link0
  • 655
  • 5
  • 17
1

You can try like this;

usercount = int(input("Up to what number do you want to go? "))
stars = '*'
for i in range(usercount):
    print(str(i+1)+stars)
    stars += '*'
Wasif
  • 14,755
  • 3
  • 14
  • 34
0

How about this one? The algorithm is the same as previous answer. This just has less code

result = "\n".join([f"{i}{'*'*i}" for i in range(1, 8)])
print(result)
Daisuke Akagawa
  • 484
  • 2
  • 9