简体   繁体   中英

Efficiency for python for loop cs50 pset6

I'm taking a free online course in edx for cs50 introduction of computer science. What I mean is that I'm beginner in this. Now I'm already in problem 6 when they introduce the python programming language. Its very simple problem.

The problem is that I think my code looks so stupid. According to the video lesson, python should increase my coding time efficiency from coding in c which is the language they taught us in previous lesson. This is not so important right now. It's just that I think that I will be using this function a lot so I hope someone will help me explain how to improve this code.

from cs50 import get_int

while True:
    height = get_int("height: ")
    if height > 0 and height < 9:
        break
n = height -1
m = 1
for i in range(height):
    for j in range(n):
        print(" ", end="")
    n -= 1
    for k in range(m):
        print("#", end="")
        if m > height:
            break
    m += 1

    print("")

I should recieve an output like this:

$ ./mario
Height: 4
   #
  ##
 ###
####

As you can see my for-loop looks really stupid, at least it looks like that to me. I think my code in c is better that this, hence the problem.

You will definitely be able to write a program faster in python than in C, when you are just as used to one language as you are to the other.

The reason for that is that python has a lot more abstractions.

You can write a solution for this problem like this in python:

from cs50 import get_int

while True:
    height = get_int("height: ")
    if height > 0 and height < 9:
        break

print('\n'.join([' ' * (height - i) + '#' * i for i in range(1, height + 1)]))

Is this much better than your code? Probably not. Sometimes trying to condense all the code in as few lines as possible isn't the best idea.

If you are writing a complex system with tens of thousands of lines of code, you will definitely notice how python makes it much easier to write code compared to C, because you need to worry about less things. But when you are writing the solution to a small exercise the difference won't be so noticeable.

It is certainly possible to shorten the code you have provided, and probably make it clearer. Eg

from cs50 import get_int

while True:
    height = get_int("height: ")
    if height > 0 and height < 9:
        break

for i in range(1, height + 1):
    print(" " * (height - i), end="")
    print("#" * i)

You can achieve what you want with the following code (no need of a double loop):

height = 4
n = height-1
m = 1

print("Result")
for i in range(height):
    print((height-i-1)*" "+(i+1)*"#")

Result:

Result
   #
  ##
 ###
####

Please bear in mind that, in general, C is a faster programming language. However, you can achieve improved performance in Python if you get used to the way of working in Python. It is also easier to code.

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