python help!!!

dark_angel

Well-Known Member
OP
Newcomer
Joined
Jul 14, 2006
Messages
67
Trophies
0
Age
33
Location
Queensland
Website
Visit site
XP
216
Country
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.
 

UltraMagnus

hic sunt dracones
Member
Joined
Aug 2, 2007
Messages
1,964
Trophies
0
Age
34
Location
Portsmouth
Website
Visit site
XP
210
Country
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.
 

dark_angel

Well-Known Member
OP
Newcomer
Joined
Jul 14, 2006
Messages
67
Trophies
0
Age
33
Location
Queensland
Website
Visit site
XP
216
Country
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.
 

dark_angel

Well-Known Member
OP
Newcomer
Joined
Jul 14, 2006
Messages
67
Trophies
0
Age
33
Location
Queensland
Website
Visit site
XP
216
Country
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.
thanks,
 

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • BakerMan
    The snack that smiles back, Ballsack!
  • Veho @ Veho:
    I thought it was an actual xBox at that price.
  • Sicklyboy @ Sicklyboy:
    I wanna grab a 360 Slim and a 360 E one of these days. Missed the boat of getting them at their lowest though, once they were discontinued. Could've got them for cheap back when I was a broke 20 something working at Target, but then again, I was a broke 20 something working at Target
  • Veho @ Veho:
    Being broke is no fun.
  • K3Nv2 @ K3Nv2:
    @Sicklyboy, $150 isn't that bad for a jtag slim on ebay
  • Veho @ Veho:
    I only wish it was actually playable.
  • Veho @ Veho:
    There's a guy on the Tube of You that makes playable mechanical arcade games out of Lego. This could work on the same principle.
  • Veho @ Veho:
    Just a couple of guys taking their manatee out for some fresh air, why you have to molest them?
  • Veho @ Veho:
    Stupid Chinese shop switched their shipping company and this one is slooooooow.
  • LeoTCK @ LeoTCK:
    STOP BUYING CHINESE CRAP THEN
  • LeoTCK @ LeoTCK:
    SUPPORT LOCAL PRODUCTS, MAKE REVOLUTION
  • LeoTCK @ LeoTCK:
    THEY KEEP REMOVING LOCAL SHIt AND REPLACING WItH INFERIOR CHINESE CRAP
  • LeoTCK @ LeoTCK:
    THATS WHY MY PARTNER CANT GET A GOOTWEAR HIS SIZE ANYMORE
  • LeoTCK @ LeoTCK:
    HE HAS BIG FOOT AND BIG DUCK
  • LeoTCK @ LeoTCK:
    d*ck i mean*
  • LeoTCK @ LeoTCK:
    lol
  • Veho @ Veho:
    Mkay.
  • Veho @ Veho:
    I just ordered another package from China just to spite you.
  • SylverReZ @ SylverReZ:
    Communism lol
  • SylverReZ @ SylverReZ:
    OUR products
  • The Real Jdbye @ The Real Jdbye:
    @LeoTCK actually good quality products are dying out because they can't compete with dropshipped chinese crap
    +1
  • BakerMan @ BakerMan:
    @LeoTCK is your partner the sascrotch or smth?
    BakerMan @ BakerMan: @LeoTCK is your partner the sascrotch or smth?