简体   繁体   English

Python中的马里奥CS50

[英]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!所以我尝试用 python 而不是 C 来编写 CS50 中的 mario 作业来挑战自己,我设法让它工作了! 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第一行应该以 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). Python range() 函数返回一个整数列表,从某个下限(默认为零)到(但不包括)某个上限,可能以某个其他数字(默认为 1)的增量(步长)返回。 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):您的输出应该类似于(例如,如果我们输入高度为 3):

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

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 .为了描述 for 循环的条件语句, i从 0 开始,一直到h

To understand how your print() statement works, think of it as a line segment that consists of spaces and hashes.要了解您的print()语句的工作原理,请将其视为由空格和散列组成的线段。 The length of this segment is h .该段的长度为h So, in h - i , you print part of this "segment" as spaces, and in i + 1 , you print the remainder of the loop.因此,在h - i ,您将此“段”的一部分打印为空格,而在i + 1 ,您将打印循环的其余部分。

Now, the reason you use i + 1 , and not just i is because of the for-loop condition;现在,您使用i + 1的原因,而不仅仅是i是因为 for 循环条件; i starts at zero. i从零开始。 As a result, you must have realized that the first line has no hashes, as there is h - 0 spaces, and 0 hashes.结果,您一定已经意识到第一行没有散列,因为有h - 0空格和0散列。 That is why the i + 1 was required;这就是为什么需要i + 1原因; 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).现在它从 1 开始(以便打印第一个散列,并且整个“段”不仅是第一行中的空格),并且它以h + 1结束(因此循环的范围保持不变)。

The i + 1 is removed; i + 1被移除; seeing the print() statement makes it easier to understand the "segment" logic discussed above.看到print()语句可以更容易地理解上面讨论的“段”逻辑。

*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 # .如果height = 1 ,我们可以看到_# ,但必须是# 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")
  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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