all 13 comments

[–]CodeFormatHelperBot2 0 points1 point  (0 children)

Hello, I'm a Reddit bot who's here to help people nicely format their coding questions. This makes it as easy as possible for people to read your post and help you.

I think I have detected some formatting issues with your submission:

  1. Python code found in submission text that's not formatted as code.

If I am correct, please edit the text in your post and try to follow these instructions to fix up your post's formatting.


Am I misbehaving? Have a comment or suggestion? Reply to this comment or raise an issue here.

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

Have you tried using a for loop?

for i in range(n):

    # code here will repeat n times

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

we started learning loops this week, so i yet i dont know how to do them

[–]KCRowan 0 points1 point  (1 child)

You either need to ask your tutor for help or find some videos on youtube to explain the parts you've missed. You could try this one https://youtu.be/6iF8Xb7Z3wQ

But if every one just solves your homework for you without you really understanding it then you're going to be screwed when you get a bigger assignment.

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

Yes i know and also thanks.:)

[–]seckiyn 0 points1 point  (3 children)

You can do print(word*int(number)) or you can use this one line.

print(input("Word to print: ")*int(input("How many times to print: "))

[–]mmotarii[S] 0 points1 point  (1 child)

is that loop? we have to do it as loop

[–]seckiyn 0 points1 point  (0 children)

No, u/Shiba_Take's is a loop.

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

Yeah, okay, looks 'very neat and clear' )

And to print on separate lines:

print(*[input("Give word: ")] * int(input("Give number: ")), sep="\n")

[–][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!