+ 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

14th Jul 2021, 8:03 AM
Student
Student - avatar
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)
14th Jul 2021, 8:30 AM
visph
visph - avatar
+ 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.
15th Jul 2021, 2:47 PM
Zen Coding
Zen Coding - avatar
+ 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
14th Jul 2021, 8:22 AM
visph
visph - avatar
+ 1
sure, but waldorf operator (:=) only works in newest versions of Python ;P
14th Jul 2021, 11:23 AM
visph
visph - avatar
+ 1
Yeah, but we came from 30 lines to 1. Well, readability is shit, but it was a nice challenge :-D
14th Jul 2021, 11:25 AM
Zen Coding
Zen Coding - avatar
+ 1
visph and Zen Coding Both of your codes seem to have bugs in them.
15th Jul 2021, 2:20 PM
Calvin Thomas
Calvin Thomas - avatar
+ 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
16th Jul 2021, 2:58 AM
Calvin Thomas
Calvin Thomas - avatar
0
Hi @Student, What's the error message?
14th Jul 2021, 8:11 AM
Zen Coding
Zen Coding - avatar
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.
14th Jul 2021, 8:20 AM
Student
Student - avatar
0
Here i is row , s is space ,and sr is stars
14th Jul 2021, 8:22 AM
Student
Student - avatar
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 ;-)
14th Jul 2021, 10:43 AM
Zen Coding
Zen Coding - avatar
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
14th Jul 2021, 10:51 AM
visph
visph - avatar
0
visph I love it! 😆 let's see if I can reduce the character count.
14th Jul 2021, 11:01 AM
Zen Coding
Zen Coding - avatar
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
14th Jul 2021, 11:22 AM
Zen Coding
Zen Coding - avatar
0
and second reversed list must have slice start at -2 ^^
14th Jul 2021, 11:24 AM
visph
visph - avatar
0
"and second reversed list must have slice start at -2 ^^" You are right. 10 Points for Gryffindor :-D
14th Jul 2021, 11:26 AM
Zen Coding
Zen Coding - avatar
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))
15th Jul 2021, 8:23 PM
Ratnapal Shende
Ratnapal Shende - avatar
- 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")
14th Jul 2021, 10:58 AM
visph
visph - avatar
- 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
14th Jul 2021, 11:04 AM
visph
visph - avatar