Really stuck on this and its supposed to be easy.

I'm supposed to input 2 positive nonzero integers into x and y. Then return the value of x times y. The book has an example of 7 * 4 = 4 + 4 + 4 + 4 + 4 + 4. What im having trouble with is coming up with the code that prints horizontally instead of vertical.

print "seomthing", 
print "something on the same line"
print "another line"

voila

Edited 6 Years Ago by ultimatebuster: n/a

1   2   3   4   5   6   7??
The book has an example of 7 * 4 = 4 + 4 + 4 + 4 + 4 + 4.

I hope that's a typo on your part, otherwise you should find another book. Sorry, couldn't pass that one up. You want to start thinking of data as bytes that you use or format however you want. So, the following are 2 more ways. I'm sure there are others.

multiplier = 7
output_str = "4"
for ctr in range(multiplier-1):
    output_str += "+4"
print output_str
#
output_list = []
for ctr in range(multiplier):
   output_list.append("4")
print "+".join(output_list)

Edited 6 Years Ago by woooee: n/a

Here is my code. Hopefully its an error but the instructor didn't say anything.

All I would need is the first line of output not the other x lines of output.

def main():

    x = input("Enter a positive nonzero integer: ")
    y = input("Enter another positive nonzero integer: ")
    

    message(x,y)

def message(x,y):
    
    if (x > 0):
        print x * ('%s + ' % y)
        message(x - 1,y)

main()

Edited 6 Years Ago by Ghostenshell: n/a

Sorry, I missed the recursion part. You want to print "y", since you are using "x" as the number of times to print, and you only print "y" once per function call. Since the function calls itself until x = 0, each call in turn will print one "y" value. There is a trailing "+" on the end when using this code. You will have to test for x==1 to get rid of it. FYI, recursion is rarely used in the real world, and is considered bad coding style by some, so you probably won't have to use this outside of the class.

def main():
    x = input("Enter an integer (the number of times to print): ")
    y = input("Enter another integer (the number to be printed): ")
 
    message(x,y)
    print     ## goto a new line

  
def message(x,y):
    if (x > 0):
        print y, "+", 
        message(x - 1,y)


main()

Edited 6 Years Ago by woooee: n/a

I have one more I need help with if anyone doesn't mind.
Supposed to get the maximum number using recursion. But I do not feel this is right.

def main():

    lists = [67,34,23,100,45,78,3000,2,5]
    maximum(lists)


def maximum(lists):
    element = 0
    big_num = 0
    while element <= len(lists) - 1:
        x = lists[element]
        if x > big_num:
            big_num = x
        else:
            pass
        element = element + 1
    print big_num

main()

You would have to return big_num. This is a modified version of the first problem, using a string to store the output. It shows passing variables. Try it and add some print statements so you know what is happening. Post back with any problems.

def main():
 
    x = input("Enter an integer (the number of times to print): ")
    y = input("Enter another integer (the number to be printed): ") 
 
    message(x,y)
    print     ## goto a new line
    ret_str = using_a_string(x, y, "")
    print ret_str[:-2]     ## strip last " +"
 
def message(x,y):
 
    if (x > 0):
        print y, "+", 
        message(x - 1,y)

def using_a_string(x, y, str_out):
    if (x > 0):
        str_out += " %d +" % (y) 
        str_out = using_a_string(x - 1, y, str_out)
    return str_out
main()

I haven't been using a ""RETURN"" statement. When I use it, the output is another prompt symbol >>> I am using python 2.6.6 Thanks for the help. Please respond to the "return" issue when time permits.

You would have to return big_num. This is a modified version of the first problem, using a string to store the output. It shows passing variables. Try it and add some print statements so you know what is happening. Post back with any problems.

def main():
 
    x = input("Enter an integer (the number of times to print): ")
    y = input("Enter another integer (the number to be printed): ") 
 
    message(x,y)
    print     ## goto a new line
    ret_str = using_a_string(x, y, "")
    print ret_str[:-2]     ## strip last " +"
 
def message(x,y):
 
    if (x > 0):
        print y, "+", 
        message(x - 1,y)

def using_a_string(x, y, str_out):
    if (x > 0):
        str_out += " %d +" % (y) 
        str_out = using_a_string(x - 1, y, str_out)
    return str_out
main()

Edited 6 Years Ago by Ghostenshell: n/a

Got it to work now... Thanks for the help. Might have one or two more problems so I'm going to keep this unsolved for now. Thanks again.

I haven't been using a ""RETURN"" statement. When I use it, the output is another prompt symbol >>> I am using python 2.6.6 Thanks for the help. Please respond to the "return" issue when time permits.

Three more problems for extra credit this is the first one.

Its supposed to sum all numbers between 1+2+3+4,...n
My problem is when I was trying the test this is the best I could come up with. When I plugged in "8" for a number it cut me short 3 digits. I thought fibonacci would work but I'm not too sure.

def main():

    numbers = input("Enter a positive non-zero integer: ")
    x = 1

    sums(numbers,x)
    

def sums(n,x):
    if x < n:
        x + 1
        n - 1
        sums(n - 1,x + 1)
    print x
    
        
main()

Just figured out the math.
1+2=3
3+3=6
6+4=10
10+5=15
15+6=21
Do not know where to plug in the actual equation.

Three more problems for extra credit this is the first one.

Its supposed to sum all numbers between 1+2+3+4,...n
My problem is when I was trying the test this is the best I could come up with. When I plugged in "8" for a number it cut me short 3 digits. I thought fibonacci would work but I'm not too sure.

def main():

    numbers = input("Enter a positive non-zero integer: ")
    x = 1

    sums(numbers,x)
    

def sums(n,x):
    if x < n:
        x + 1
        n - 1
        sums(n - 1,x + 1)
    print x
    
        
main()

Here is one functioning print of all sums recursively with generators:

def rec_sums(x):
    if x<=1: this_sum = x
    else:
        this_sum = x+next(rec_sums(x-1))
    print x,'->',this_sum
    yield this_sum

next(rec_sums(10))

Thanks!!! This is what I came up with.

def main():

    numbers = input("Enter a positive non-zero integer: ")

    adit = sums(numbers)
    print adit

def sums(n):
    if n == 1:
        return n
    else:
        return n + sums(n - 1) 
main()

So you only need the final result as return value. I would not use plural names like you do and I prefer in this kind of alternative returns if.. else one liner:

def main():
    number = int(raw_input("Enter a positive non-zero integer: "))
    print rec_sum(number)

def rec_sum(n):
    return n if n == 1 else n + rec_sum(n - 1) 

main()

Edited 6 Years Ago by pyTony: n/a

OK Thanks for that. This is the last one and I'll mark as solved. I have it down to the return and the return is where I always have problems. Supposed to raise number to power of expo.

def main():

    numbers = input("Enter a positive non-zero integer: ")
    expo = input("Enter exponent: ")
    

    adit = sums(numbers,expo)
    #print adit 

def sums(n,b):
    if n == 1 or b == 0:
        return 1
    elif b == 0:
        return n
    elif n == 0:
        return ValueError, 'Function can not be preformed'
    elif (b > 0):
        sums(n*n, b-1)
    print n
    #return n
    #else:
        #return sums(n * n, b - 1) 
main()

Edited 6 Years Ago by Ghostenshell: n/a

You only need the base case that anything to power of 0 is 1 and otherwise you just calculate it with help of result of one lower exponent value. Do by writing to paper some low value like 2**3 to understand the principle.

This question has already been answered. Start a new discussion instead.