r/pythonhelp • • Feb 01 '24

Can someone explain this code for me from Elements of Programming.

1 Upvotes

I am trying to understand the DP solution for a variant of the climbing stairs problem with k steps instead of the regular 2. This is the solution in Elements of programming but I'm having trouble understanding the for loop inside the sum function. I've solved it myself but would like to understand this way. Some solutions in the book are more advanced than what i've seen. Thanks!

def number_of_ways_to_top(top, maximum_step):
    number_of_ways_to_h = [0] * (top + 1)

    def compute_number_of_ways_to_h(h):
        if h <= 1:
            return 1
        if number_of_ways_to_h[h] == 0:
            number_of_ways_to_h[h] = sum(
                compute_number_of_ways_to_h(h - i)
                for i in range(1, min(maximum_step, h) + 1)
            )
        return number_of_ways_to_h[h]

return compute_number_of_ways_to_h(top)

r/pythonhelp • • Feb 01 '24

Code stopped working

1 Upvotes

Why has my code all of a sudden stopped working? It normally did and now it doesnt im gonna send my code but its the “loan” part that isnt working I will send the point which im struggling.

loanconfirm=input('wouldyouliketotakeoutaloan?youmustpayitback')ifloan.lower()=='yes':loan=int(input('howmuchmoneydoyouwanttotakeout?')) balance=balance+loanprint(f'{loan}+{balance}={int(loan+balance)}')


r/pythonhelp • • Feb 01 '24

Need advice in python programm

1 Upvotes

Hello,

this is my python script

command = "ps ax | grep 'openvpn --daemon' | awk '{print $1}'"
process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
if out:
print ("standard output of subprocess:")
if err:
print ("standard error of subprocess:")
print ("returncode of subprocess:")

But I get this error

standard error of subprocess:
returncode of subprocess:

why do i get an error ? normally it should display me the pid of the programm openvpn