+ 2
Help me to correct the code of arrow pattern my first half of the code is error free but in second half their is error.
Print the following pattern for the given number of rows. Assume N is always odd. * * * * * * * * * * * * * * * * My code- n= int(input()) firsthalf = n+1//2 secondhalf = n//2 i=1 while i<=firsthalf: s=1 while s<=(i-1): print(' ',end='') s=s+1 sr=1 while sr<=i: print('* ',end='') sr=sr+1 print() i=i+1 i=secondhalf while i>=1: s=1 while s<=(secondhalf-i): print(' ',end='') s=s+1 sr=1 while sr<=(secondhalf-i+1): print('* ',end='') sr=sr+1 print() i=i-1
19 Respuestas
+ 3
# same could be done shorter:
n = int(input())
half = n+1//2
for i in range(half+1):
print(' '*(i-1)+'* '*i)
for i in range(half-1,0,-1):
print(' '*(i-1)+'* '*i)
+ 2
Share your findings. Is it about not having a single biggest line but two? I wasn't sure about the specification. But having a deeper look, I would play around with the index constants a little bit.
+ 1
n= int(input())
firsthalf = n+1//2
# secondhalf = n//2 # pointless
i=1
while i<=firsthalf:
s=1
while s<=(i-1):
print(' ',end='')
s=s+1
sr=1
while sr<=i:
print('* ',end='')
sr=sr+1
print()
i=i+1
i=firsthalf-1 # bad start value
while i>=1:
s=1
while s<=i-1: # bad logic
print(' ',end='')
s=s+1
sr=1
while sr<=i: # bad logic
print('* ',end='')
sr=sr+1
print()
i=i-1 # i =i-i result to 0
+ 1
sure, but waldorf operator (:=) only works in newest versions of Python ;P
+ 1
Yeah, but we came from 30 lines to 1. Well, readability is shit, but it was a nice challenge :-D
+ 1
visph and Zen Coding Both of your codes seem to have bugs in them.
+ 1
Zen Coding Here's a one-liner:
for i in range(1-(k:=(int(input())+1)//2), k):print(" "*(k-abs(i)-1)+"* " *(k-abs(i))+"\n")
Using a lambda, without the walrus operator:
print("\n\n".join((lambda x: [" "*(x-abs(i)-1)+"* "*(x-abs(i)) for i in range(1-x, x)])((int(input())+1)//2)))
Using the extending method:
print(*((k:=[" "*x + "* "*(x+1) for x in range((int(input())+1)//2)])+k[-2::-1]), sep="\n\n")
# Hope this helps
0
Hi @Student,
What's the error message?
0
Actually on an input of 7 the the output must be
*
* *
* * *
* * * *
* * *
* *
*
But my output is this
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
*
To write this code I have divided this pattern into two so, my first-half 4 lines are correct but second-half code is incorrect I don't understand what mistake I have made.
0
Here i is row , s is space ,and sr is stars
0
First:
>> firsthalf = n+1//2
That is not the first half, it's just n because 1//2=0
Example:
>> 3+1//2
3
Second:
Don't calculate everything twice. Use a list and print it first from the start and then from the end.
>>>str_list = [' '*(a-1)+'*'*a for a in range(n//2+1)]
>>>str_list.extend(str_list[-2::-1])
>>>for item in str_list:
>>> print(item)
Even shorter then @visph ;-)
0
Zen Coding I know that you could do it even shorter and even one lined ^^
I just has shown to OP how shorter its code, without introducing more new stuff wich could confuse him (ie: list comprehension) ;P
0
visph I love it! 😆 let's see if I can reduce the character count.
0
Something like that? @visph? :-D
print(*(k:=[" "*(i-1)+"* "*i for i in range(int(n)//2+1)]), *k[::-1], sep='\n')
Using a 3.8 feature: https://docs.python.org/3/whatsnew/3.8.html#assignment-expressions
0
and second reversed list must have slice start at -2 ^^
0
"and second reversed list must have slice start at -2 ^^"
You are right. 10 Points for Gryffindor :-D
0
# A real Arrow 😛😜
n=8
for i in range(n+1):
if i==n:
print("-"*24,end="")
print("*"*(i-1))
else:
print("\t\t"+' '*i+"*"*(i-1))
for i in range(n+1,0,-1):
if i==n+1:
print("-"*(24),end="")
print("*"*(i-1))
else:
print("\t\t"+' '*i+"*"*(i-1))
- 2
n = int(n)//2
print(*(" "*(i-1)+"* "*i for i in range(n+1)), *(" "*(i-1)+"* "*i for i in range(n,0,-1)), sep="\n")
- 2
Zen Coding you can do the half list directly by inputing inside range, and print destructured list, destructured reversed list minus one item... but that's less "one lined" (if you doesnt count the n input as a line of code) ;P