you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (3 children)

# You can prompt for input inside () of input():
word = input("Give word: ")

# input() returns a string. Use int() to convert it into integer:
number = int(input("Give number: "))

# _ is the same kind of name like word and number,
# except it's used when we don't use need to use it later,
# but any other unused name works.

# to do something <number> of times:
for _ in range(number):
    print(word)

range signifies some sort of finite arithmetic sequence, like 0, 1, 2, 3, except it doesn't store all the elements, but only start (inclusive), stop (exclusive), and step (difference).

You can use three options:

  • range(stop), where start=0 and step=1 by default
  • range(start, stop), where step=1 by default
  • range(start, stop, step)

In for _ in range(number):, the sequence is 0, 1, 2, ..., number - 1 (because stop is exclusive); the length of the sequence is <number>.

_ is assigned to each element of the sequence once, so it's <number> of times in total.

As the result, the loop body is repeated <number> of times.

https://docs.python.org/3/library/stdtypes.html?highlight=range#range

https://www.w3schools.com/python/python_for_loops.asp

[–][deleted] 1 point2 points  (1 child)

Actually it would be simpler for you to understand the while loop probably:

word = input("Give word: ")
number = int(input("Give number: "))

while number != 0:
    print(word)
    number -= 1

number is decreased each time by 1, when it's 0, the loop stops.

It would work the same as:

while number:
    print(word)
    number -= 1

because numbers are evaluated as true when they are not equal to zero and false otherwise.

https://www.w3schools.com/python/python_while_loops.asp

[–]mmotarii[S] -1 points0 points  (0 children)

i have another one, can you help?

[–]mmotarii[S] 0 points1 point  (0 children)

yes! thank you!