Python Forum
Help needed for a program using loops - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Help needed for a program using loops (/thread-40658.html)



Help needed for a program using loops - PythonBoy - Sep-03-2023

I got a homework to create a program whose output is like this:

*
**
***
****

I made use of for loop to make this program
My code:
n = 1
for x in range(n):
    print(x*"*")
    n = n+1
But this I'm not getting any output at all.

Please help me


RE: Help needed for a program using loops - Yoriz - Sep-03-2023

A range of 1 will only loop once with a value of 0.
0*"*" won't print anything
n = n+1 does nothing to alter your print statement as it is only using x
range can have its first parameter as the start and the second as the end, try playing around with using range(start_value, end_value) (not end value is not inclusive) to get the output you desire.
Note: the line n = n+1 can be removed.


RE: Help needed for a program using loops - PythonBoy - Sep-03-2023

Thanks very much, I finally understand what the problem was. I finally fixed it.
for x in range(5):
    print(x*"*")



RE: Help needed for a program using loops - Yoriz - Sep-03-2023

Well done, you will still be getting a blank line printed as the range will return 0, 1, 2, 3, 4.
Changing the range to have a start value of 1 will skip the o giving 1, 2, 3, 4.
for x in range(1, 5):
    print(x*"*")



RE: Help needed for a program using loops - purush167 - Sep-10-2023

try this code

num = int(input("Please enter number of times: "))

for numb1 in range(1, num):
print ("X" * numb1)


RE: Help needed for a program using loops - PythonBoy - Sep-10-2023

(Sep-10-2023, 06:36 AM)purush167 Wrote: try this code

num = int(input("Please enter number of times: "))

for numb1 in range(1, num):
print ("X" * numb1)

Yes, this works too.
But here I see that the number of times it prints is in the hand of the user. That is also useful

Thanks!