简体   繁体   中英

Python: I'm confused with spaces in between characters

So I am outputting a triangle made of characters inputted by the user, and the user also inputs the height of the triangle (height equals base as well).

The program also wants me to have spaces in between each character printed. I have two methods that will correctly output what I want. I just want to understand what the space = space + value... line does.

I only figured out how to do it because I knew I had to use a "for" loop and pretty much just messed around with which variables I placed in the loop.

sorry there are so many questions, loops confuse me so much

triangle_char = input('Enter a character:\n')
triangle_height = int(input('Enter triangle height:\n')) 
space = ''

for i in range(0, triangle_height, 1):   
    for value in triangle_char:          #if this loop is not here, and I do print(i * triangle_char), it will only output a triangle with height of 2. why?
        space = space + value + ' '      #what does this do? 
        print(space)

#beneath is the 2nd version of the code

counter = 1
space = ''
for value in range(triangle_height):
    while counter <= triangle_height:
        for value in triangle_char:      #how can there be a value in just a character?
            space = space + value + ' '    
            print(space)

TLDR: read this

Line 5

range(a, b) returns integers from the range [a, b). So in line 4 your code will loop i starting from 0 up to triangle_height-1 . Therefore, on its first iteration when you print 0 * triangle_char it would print an empty string (nothing).

Also note that the for loop on this line is redundant. When looping through a string of what is expected to be length 1, it would only assign value to triangle_char .

Line 6

space = space + value + ' ' concats the value of value + ' ' to space. This effectively adds the value triangle_char and a space to the end of space .

Line 15

Unlike many lower-level languages, there are no chars in python. Rather, python treats all characters as strings. Therefore this line would be looping through the characters of a length 1 string.

What does space = space + value + ' ' do?

space = space + value + ' ' will add value and a space ( ) to the value of the variable space and then overwrite the old value of space with this new one.

Perhaps naming the variable differently would help, eg

# create a blank message to start off with
message = ""
# add the user-entered value and a space to the message
message = message + value + ' '
# print out the message
print(message)

Why does print(i * triangle_char) only output a triangle of height 2?

I suspect that you entered a value of 3 for the triangle height when you saw this. If you entered a height of 5 you will see 5 lines, but the first will be blank because on the first iteration of the loop i is 0 and 0 * triangle_char will be a blank string. So the apparent height of the triangle will always be one less than triangle_height . Here is an example with height of 5.

>>> triangle_char = "+"
>>> triangle_height = 5
>>> for i in range(0, triangle_height, 1):
...     print(i * triangle_char)
     <=== this is the first blank line
+
++
+++
++++

How can there be a value in just a character?

A character in Python is really a string of length one. Python allows you to iterate/loop through strings and when you do you go through all the characters in the string one at a time. Therefore a "character" has one value when you loop over it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM