python help!!!

Discussion in 'General Off-Topic Chat' started by dark_angel, Mar 8, 2009.

  1. dark_angel

    dark_angel Advanced Member

    Jul 14, 2006
    so i just started my programming course at uni and i just need help with i question i've been stuck on for about 2 hours now.
    i know that there are a lot of bright minds in this community that can program. so this is the first place i came to for help.
    the question is:
    Create a procedure called printSquareStar(), using printNString(string, numTimes), that prints the following using printNString.


    This is a 4x4 square made up of the star (*) character.
    i was given this info if it helps at all, but it shouldn't be anything new to the more veteran programmers.
    # Useful Operators: Equals ==
    # Increment +=

    * Eg. i = i + 1 -> i+=1

    You should use the function printNString, that takes a string and a number and prints it n times. Eg:

    printNString('a', 5) => 'aaaaa'

    Aside: For testing your solutions in IDLE you might like to use the following definition. It is NOT necessary to include this definition in your solution to the problems below.

    def printNString(a, n):
    print n*a

    Note, however, that the above example is unusual. Most of the functions you need to define in these on-line problems do not use print but instead return a value using return.
    here's what i wrote:
    def printSquareStar():
    ----def printNString(a, n):
    --------i = 0
    --------while int(n) != i:
    ------------print a
    ------------i = i + 1
    ----printNString('****', 4)
    but i keep getting this error:
    Here are the results of your input on the test cases:

    Test case is:

    Your input produces:

    the correct result is:

    wrong - TRY AGAIN
    now i don't know what to do.
    please help.
  2. UltraMagnus

    UltraMagnus hic sunt dracones

    Aug 2, 2007
    I don't know python, but I will give it a try. count yourself lucky, in the first year of my uni course we have to learn C, ASM and VDHL

    I would do it something like

    while (i =< n) {
    printf( %c, a)
    i = i + 1
    printf ("\n")

    you then need to put that in a function that is called with "*" and 4 as its input and call it four times.
  3. dark_angel

    dark_angel Advanced Member

    Jul 14, 2006
    thanks for the reply ultramagnus, but i ended up getting it after an extra 3 hours of trial and error.
    once i figured that out almost every other question after that just became too easy.
    although your code didn't work in python, i still appreciate you trying to help.
  4. dark_angel

    dark_angel Advanced Member

    Jul 14, 2006
    after having figured out the last problem, i'm kind of stuck again.
    the question is:
    Define a procedure p2(n) that takes an integer parameter n. If n is greater than 1, the procedure returns the largest power of two that is less than n; otherwise, it returns 0.
    Hint: You will need to use a while loop. Here is an example of a function that adds the numbers from 1 to n

    def sumto(n):
    >>>>sum = 0
    >>>>m = 1
    >>>>while m >>>>>>>sum = sum + m
    >>>>>>>>m = m + 1
    >>>>return sum

    For your problem you will need to take increasing powers of 2 until that power exceeds n.
    this was information that i was given if it helps at all.
    the problem shouldn't involve any printing.(i.e. you shouldn't use print function).

    The question asks you to return the largest power of two that is less than n
    provided that n is greater than 1. If n is not greater than 1, you should return 0.

    Hence, the code can be structured as 'if.....else'(there are other ways).
    Firstly, use 'if test' to test if n(the only argument that this function takes) is
    greater than 1. If n is greater than 1, we return the value requested(largest
    value that is less than n). If not, the block of code under 'if' statement is
    skipped, then we return 0 under 'else' statement.

    The largest power of 2 can be computed by repetitively doubling 1 or 2 using a
    while loop. One thing to note is that you might need to half the final return
    value, but this depends on how you set up the comparison test for the while loop.
    this is what i wrote:
    def p2(n):
    >>>>if n >>>>>>>return 0
    >>>>elif n > 1:
    >>>>>>>>i = -1
    >>>>>>>>while (i >>>>>>>>>>>i += 1
    >>>>>>>>>>>>p = 2**i
    >>>>>>>>if p < n:
    >>>>>>>>print p
    and that prints all the values lower than n, but a can't get it to return only the highest value, that's closest to n.
    i only have an hour left remaining on this question. so i need help fast. if anyone knows please help.