Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating strings with spaces using Python

When I run this code it behaves like expected:

x = int(input("Put number: "))
result_figure =[]
xtempleft = x-1
xtempright = 0
space = " "
sl = "/"
bsl  = "\\"
#Q1
for i in range(x):
    if xtempleft > 0:
        q1= space * xtempleft + sl
        xtempleft -= 1
        print(q1)

    else:
        q1 = sl
        print(q1)
#Q2
for i in range(x):
    if xtempright == 0:
        xtempright += 1
        q2= bsl 
        print(q2)
    else:
        q2 = space * xtempright + bsl
        xtempright += 1
        print(q2)

I get this:

    /
   /
  /
 /
/
\
 \
  \
   \
    \

The problem is that when I try to do some modification:

for i in range(x):
    result =""
    if xtempleft > 0:
        q1= space * xtempleft + sl
        xtempleft -= 1
        result += q1     
    else:
        q1 = sl
        result += q1
#Q2
    if xtempright == 0:
        xtempright += 1
        q2= bsl 
        result += q2
    else:
        q2 = space * xtempright + bsl
        xtempright += 1
        result += q2  
    print(result)

to print what I need in the same line i get it like spaces from Q2 disappeared somewhere and didn't concatenate.

    /\
   / \
  /  \
 /   \
/    \

Anyone could help me with this? I have tried in many ways and can't get it.

like image 288
Emejcz Avatar asked Nov 22 '25 16:11

Emejcz


1 Answers

In modification you omitted the for loop.

In your initial code there are two for loops and in your modification you omitted the second for loop.

like image 56
Rahul Avatar answered Nov 25 '25 06:11

Rahul