Python Forum
Help needed for a program using loops
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed for a program using loops
#1
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
Reply
#2
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.
PythonBoy likes this post
Reply
#3
Thanks very much, I finally understand what the problem was. I finally fixed it.
for x in range(5):
    print(x*"*")
Reply
#4
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*"*")
PythonBoy likes this post
Reply
#5
try this code

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

for numb1 in range(1, num):
print ("X" * numb1)
Reply
#6
(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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Program that allows to accept only 10 integers but loops if an odd number was entered gachicardo 4 3,636 Feb-24-2022, 10:40 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020