Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
i = 10
while i > 0:
print i*'*' + 2*(10-i)*' ' + i*'*'
i -= 1
for x in range(2,11):
print x* '*' + 2*(10-x)*' '+ x*'*'
x += 1
Can someone tell me what each line does?
I'll keep it very simple, because this is very basic stuff.
i = 10
Variable i is initialised as 10.
while i > 0:
print i*'*' + 2*(10-i)*' ' + i*'*'
i -= 1
While variable i is bigger than 0, it prints the string * i times, an empty space 2 * (10 - i) times and then the string * i times again. Every loop it subtracts 1 off of i, so i starts out as 10 and goes all the way down to 1. This results in the following triangle / pyramid:
********************
********* *********
******** ********
******* *******
****** ******
***** *****
**** ****
*** ***
** **
* *
for x in range(2,11):
print x* '*' + 2*(10-x)*' '+ x*'*'
x += 1
Here the variable x starts off as 2 and increments all the way to 10, which results in an upside down version of the above triangle (not exactly, but it would if you used range(1, 11) instead). Also x += 1 is redundant here, as the range function will already increment x in steps of 1 (because the step argument is omitted). You can see this for yourself by running the following code:
for x in range(1, 11):
print x
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
Define a function named print_pyramid(number_of_rows) which takes an integer as a parameter and prints a specific pyramid pattern using numbers. Note: you may assume that the number of rows will always be > 1 and < 10 and you must use a nested for loop.
For example:
Pyramid I'm meant to get
Above is my question and what I need to get, but I am unsure on how to complete it below is my code attempt and my result.
This is my code:
def print_pyramid(number_of_rows):
for row in range(number_of_rows):
for column in range(row, number_of_rows):
print("", end="")
for column in range(row+1):
print(row+1, end="")
for column in range(row, number_of_rows-1):
print(" ", end= "")
for column in range(row+1):
print(row+1, end="")
print()
But my code gives me this:
My result/output
Jared's answer is correct and definitely more elegant, but here's another solution anyway. More beginner friendly hopefully easier to understand at a glance.
def pyramid(number_of_rows):
for i in range(1, number_of_rows + 1):
indent = ' ' * (number_of_rows - i)
row = str(i) * (i * 2 - 1)
print(indent + row)
here is a list comprehension that uses a for loop to generate the pyramid pattern
print_pyramid = lambda x : print("\n".join([(" "*(x-i))+(str(i)*(i*2-1))+(" "*(x-i)) for i in range(1,x+1)]))
print_pyramid(9) gives:
1
222
33333
4444444
555555555
66666666666
7777777777777
888888888888888
99999999999999999
You can define a function to print your pyramid in this way
def pyramid(rows: int) -> str:
if rows < 1 or rows > 9:
raise ValueError
pyramid_string = ""
for i in range(1, rows + 1):
pyramid_string += f" " * (rows - i) + f"{i}" * (i * 2 - 1) + "\n"
return pyramid_string
then call the function
print(pyramid(rows=5))
or you can use a variable to store the result of the function
five_rows_pyramid = pyramid(rows=5)
print(five_rows_pyramid)
output
1
222
33333
4444444
555555555
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
Does anybody of you know something like JetBrains Academy? Could You help me with this task? I know surely it is very simple but I really cannot do it. I have tried a lot of times.
https://hyperskill.org/learn/step/6715
Only thing that changes in the square are the insides, so just check if you're at the first line or last line.
Not the most efficient but python makes it easy.
size = 4
for i in range(size): # each loop prints a row
print(*
['*'] + # first star in the row
['*' if i in [0, size-1] else ' '] * (size-2) + # inside stars of the row
['*'] # last star in the row
)
* * * *
* *
* *
* * * *
The task creates a square of size 4 by 4.
Try this :
size = 4
for i in range(size):
if i == 0 or i == size-1:
var = "* " * size
else:
var = "* " + " " * (size-2) + "*"
print(var)
Output:
* * * *
* *
* *
* * * *
Now you can change the value of variable size and play around to make square shapes of any dimension.
I'm trying to hone my Python 3 (specifically, nested loops) with the good old triangle exercises (which only takes an odd input for now). However, I have come across a problem I can't warp my head around with.
user_input = 7
x = 1
temp = user_input
spaces = " "
stars = ""
y = temp - 2
t = 0
while x < temp:
while y > t:
stars = "*" * x
spaces = spaces * y
print(spaces + stars)
spaces= " "
y -= 1
x += 2
I have a user_input (which is 7 for now so I don't have to give an input each time I run).
A variable x and t used for the while loop
Another temporary variable that holds my user_input (in case I decrement it as not to 'damage' the original variable).
A variable spaces and another variable stars (which should be self explanatory as I'm trying to draw a triangle based on asterisks).
I have a variable y which is equal to temp - 2
Expected output of 7 should be like this:
*
***
*****
*******
The reason I have made y equal to temp - 2 is because the first row has spaces equal to user_input - 2.
So let's say our input was 7, first row's amount of spaces is 5.
If our input is 9, first row's amount of spaces is 7
The first while loop will loop from 1 to 7 (1, 3 , 5, 7) which is why I made x equal to 1.
The second while loop should loop from input - 2 until 0.
Weird thing is, that if my user_input is equal to 5, it outputs just how I expected it to be.
*
***
*****
But once I enter something like 7, it builds a triangle from 1 to 9 (1, 3, 5, 7, 9)
*
***
*****
*******
*********
But I expect it to end right before the last row, it should output as many asterisks as my input.
Is my thought process wrong? If so, where did I end up wrong?
I hope I have clarified everything as much as possible.
Thanks a bunch.
Seems overly complicated. Why not just:
input = 7
i = 1
while i <= input:
spaces = ' ' * ((input-i) // 2)
stars = '*' * i
print(spaces + stars)
i += 2
*
***
*****
*******
Or even simpler, using str.center:
while i <= input:
print(('*' * i).center(input))
i += 2
Let's clarify a bit your code a bit :
t is useless since it holds only 0 and never changes, use 0 instead
user_input is never used except to make temp = user_input, use user_input instead of temp. As for decrementing it, it doesn't happen, and anyway you never return it to the user so it would be ok.
that's kind of a typo so it's ok, but avoid letting your debugging prints like print(x,y) when you show some code on Stack Overflow, it's harder for us to understand the whole code.
If you change back spaces = " " at the end of the while, just use spaces = " " * y.
You do nothing between the two while so you can "merge" them with an and on conditions.
So now we have :
user_input = 9
x = 1
y = user_input - 2
while x < user_input and y > 0:
stars = "*" * x
spaces = " " * y
print(spaces + stars)
y -= 1
x += 2
As you can see, you have two stopping conditions on your while when only one would be clearer. The reason behind your code works with 7 and not more is because 7 is the limit between when one condition stops loop and when the other does.
I would suggest to change your code to :
user_input = 3
x = 0
while x < user_input//2:
stars = "*" * (x * 2 + 1)
spaces = " " * (user_input//2 - x)
print(spaces + stars)
x += 1
There is a bug in your code. Here is the rectified code.
user_input = 7
x = 1
temp = user_input
spaces = " "
stars = ""
y = temp - 2
t = 0
while x <= temp:
stars = "*" * x
spaces = spaces * y
print(spaces + stars)
spaces= " "
y -= 1
x += 2
It is not necessary to check y>0 since your first while loop is enough to meet the requirement. Because of that extra while loop you are getting ambiguous values of (x,y).
The lazy solution using the builtin center() and the format mini language:
user_input = [5,7,9]
def getStars(num):
return ('*' * i for i in range(1,num+1,2))
def sol1(num):
for s in getStars(num):
print(s.center(num))
def sol2(num):
stars = getStars(num)
for s in stars:
print( ("{:^"+str(num)+"}").format(s))
for s in user_input:
sol1(s)
sol2(s)
Output:
*
***
*****
*
***
*****
*
***
*****
*******
*
***
*****
*******
*
***
*****
*******
*********
*
***
*****
*******
*********
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Today, i have trying to resolve a small star pyramid :
Input:
5 1
Output:
*
**
***
****
Code:
x = 1
y = 0
m, e = map(int, raw_input().split())
while x < m:
print "\n" * y, "*" * e
m -= 1
e += 1
I did that but there is a better solution?? Thanks =)
I think this can be solved more easily:
stop, first = map(int, raw_input().split())
for i in range(stop - 1):
print '*' * (i + first)
just for fun >:)
class c:
def __init__(s,m,e):
s.e , s.m = sorted([e, m])
s.r = 42
def __iter__(s):
return s
def next(s):
if s.m < s.e:
t = "".join(chr(s.r) for _ in range(s.m))
s.m += 1
return t
else:
raise StopIteration
print "\n".join(c(*map(int,raw_input().split())))
n = int(raw_input())
for i in range(n): print "*"*i
This appears to do what your program intends to do, however I can't quite tell because of the issues I raised in my comment above.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Can anyone tell me why this has a syntax error? I've run this exact code before and it worked perfectly. The line in strong text is where Python tells me the syntax error is. Thanks, everyone!
import random
count = 0
while count < 10:
attackerLV = 20
attackerST = 20
attackerSK = 20
baseAtkPwr = 20
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100
**defenderLV = 20**
defenderCON = 20
defenderSKa = 20
baseDefPwr = 20
defensePWR = (((random.randint(85,100)) * (baseDefPwr + defenderLV + defenderCON + defenderSKa)) // 4) // 100
damage = attackPWR - defensePWR
if damage <= 1:
damage = 1
print(str(attackPWR))
print(str(defensePWR))
print(str(damage))
print()
count = count + 1
You missed a parenthesis here:
attackPWR = ((random.randint(85,100) * (baseAtkPwr + attackerLV + attackerST + attackerSK)) // 100