简体   繁体   中英

Mario CS50 in Python

so I tried coding the mario assignment from CS50 in python instead of C to challenge myself and I managed to get it to work! I don't quite understand how the loops are working, especially the math because I just played around to get the result I wanted.

The requirements of the assignment were as follows

  1. Create a half pyramid using hashes
  2. Make sure it's right aligned
  3. It should correspond to the height given by the User
  4. The first line should start with 2"#"s

Any clarification on my loops would be greatly appreciated :)

print('Height: ', end='');
h = int(input());

while(h<0 or h > 24):
  print("That is an invalid input")
  print('Height: ', end='');
  h = int(input());

for i in range(h):
    print(" "*(h-i)+"#"*(i+1));

range(start, stop[, step])

Python range() function returns a list of integers from some lower bound (zero, by default) up to (but not including) some upper bound, possibly in increments (steps) of some other number (one, by default). Hence,

range(3) returns a sequence: 0, 1, 2
range(1,3) returns a sequence: 1, 2
range(1,7,2) returns a sequence: 1, 3, 5

Firstly, just wanted to point out, your code does not print out the entire pyramid; it only prints half of it out. Your output should something like (if we input height to be 3, for example):

  # #
 ## ##
### ###

Your code only prints the left side:

  #
 ##
###

Now, to your question. Here is how you got it to work:

 for i in range(h): print(" " * (h - i) + "#" * (i + 1));

To describe the conditional statement of the for-loop, i starts at 0, and it goes up till h .

To understand how your print() statement works, think of it as a line segment that consists of spaces and hashes. The length of this segment is h . So, in h - i , you print part of this "segment" as spaces, and in i + 1 , you print the remainder of the loop.

Now, the reason you use i + 1 , and not just i is because of the for-loop condition; i starts at zero. As a result, you must have realized that the first line has no hashes, as there is h - 0 spaces, and 0 hashes. That is why the i + 1 was required; you are printing an extra space at the start of every line, if you check closely with your current code.

A more easier-to-understand logic of your code, with the same results, is:

for i in range(1, h + 1): # The counter's range is toyed with here
    print(" " * (h - i) + "#" * i); # The "i + 1" is removed

Here, the range of the loop is toyed with; now it starts at 1 (so that the first hash is printed, and the entire "segment" is not only spaces in the first line), and it ends at h + 1 (so that the range of the loop stays the same).

The i + 1 is removed; seeing the print() statement makes it easier to understand the "segment" logic discussed above.

*Just to clarify, the alternate code I provided above is not intended as a replacement to your code; I just included it to reinforce my "segment" idea. Your code is perfectly fine and works just as well as mine.

i think, it isn't right answer. For understanding it, use "_" or other symbols instead spaces. If height = 1 , we can see _# , but must be # . So right answer, can be:

print('Height: ', end='');
h = int(input());

while(h<0 or h > 24):
  print("That is an invalid input")
  print('Height: ', end='');
  h = int(input());

for i in range(h):
     print("_" * (h - (i+1)) + "#" * (i + 1));

sorry for my English.

You'd want to break them up into two so you can see the picture clearly.From there the side by side implementation will be easier.

left_align_counter =  0
print("Printing  left aligned")
print("Height: ", end="")
height =  int(input())

for h in range(height):
    left_align_counter +=1 
    print("#"*left_align_counter)

print("DONE.")

right_align_counter = 0
print("Printing right align ")

for h in range(height):
    right_align_counter += 1
    print(" "*int(height-right_align_counter), end="")
    print("#"*int(right_align_counter))
    
print("DONE.")

Printing side by side

left_align_counter =  0
print("Printing  left aligned")
print("Height: ", end="")
height =  int(input())

for h in range(height):
    left_align_counter +=1 
    print("#"*left_align_counter)

print("DONE.")

right_align_counter = 0
print("Printing right align ")

for h in range(height):
    right_align_counter += 1
    print(" "*int(height-right_align_counter), end="")
    print("#"*int(right_align_counter))
    
print("DONE.")

#here we can use any counter for the blocks be it right or left so just use counter variable
counter = 0
for h in range(height):
    counter += 1
    print(" "*int(height-counter), end="")
    print("#"*int(counter), end=' ')
    print("#"*int(counter),end="\n")
  

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