Hi, I'm working in the following project:

Write a function in python that displays at the left margin of the screen a solid square of asterisks whose side is specified in integer parameter side. For example, if side is 4, the function displays

****
****
****
****


I did this program before in C++ but now I need to do it in PYTHON PROGRAMMING and I'm not sure how to do it. Can anybody please help me to do this function in Python?THANKS A LOT!!!!

P.D: This is how I previously did it in C++, but unfortunately, Python is completely different :(

//This says put an asterix on a line size times for(int i=0; i<size; i++) { cout<< '*' << endl; }} //This says put size number of asterixes on one linefor(int i=0; i<size; i++) { cout<<'*'; } cout << endl;} //If you complete the blank line in the following snippet correctly it will put size asterixes on a line and repeat it for size lines /*put code in the blank line immediately below this and before the first { to control how many lines are written*/ { //this line controls how many asterixes per line for(in i = 0; i < size; ++i) { cout<<'*'; } //this starts a new line cout << endl; }}//This says put an asterix on a line size times
for(int i=0; i<size; i++)
{
cout<< '*' << endl;
}
}

//This says put size number of asterixes on one line
for(int i=0; i<size; i++)
{
cout<<'*';
}
cout << endl;
}

//If you complete the blank line in the following snippet correctly it will put size asterixes on a line and repeat it for size lines

/*put code in the blank line immediately below this and before the first { to control how many lines are written*/

{
//this line controls how many asterixes per line
for(in i = 0; i < size; ++i)
{
cout<<'*';
}
//this starts a new line
cout << endl;
}
}

Maybe:

for a in range(20):
  print "*" * 10

Multiplication table:

i = 0
while i <= 10:
  j = 0
  while j <= 10:
    print ("%4d" % (i * j)),
    j += 1
  print
  i += 1

Edited 6 Years Ago by -ordi-: n/a

I just tried it but it gives me the following error:

Traceback (most recent call last):
File "C:/Python26/stars.py", line 4, in <module>
while i <= 10:
NameError: name 'i' is not defined

Please advise. Thanks

I just tried it but it gives me the following error:

Traceback (most recent call last):
File "C:/Python26/stars.py", line 4, in <module>
while i <= 10:
NameError: name 'i' is not defined

Please advise. Thanks

Copy correctly!

Are you sure that i = 0 is your code?

Edited 6 Years Ago by -ordi-: n/a

Hi ordi,

Thanks for helping me out. I really appreciate it! I just tried it and it works but the output that I am suppose to get is a little different. For example, if side is 4, the function should displays

****
****
****
****
I tried to change the "for loop " to 4 and this is what I got:
**********
**********
**********
**********
0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10
0 2 4 6 8 10 12 14 16 18 20
0 3 6 9 12 15 18 21 24 27 30
0 4 8 12 16 20 24 28 32 36 40
0 5 10 15 20 25 30 35 40 45 50
0 6 12 18 24 30 36 42 48 54 60
0 7 14 21 28 35 42 49 56 63 70
0 8 16 24 32 40 48 56 64 72 80
0 9 18 27 36 45 54 63 72 81 90
0 10 20 30 40 50 60 70 80 90 100

Do you know how can I modify the function in a way that I can get the first output? Thank you so much!

Hi ordi,

Thanks for helping me out. I really appreciate it! I just tried it and it works but the output that I am suppose to get is a little different. For example, if side is 4, the function should displays

****
****
****
****
I tried to change the "for loop " to 4 and this is what I got:
**********
**********
**********
**********
0 0 0 0 0 0 0 0 0 0 0
0 1 2 3 4 5 6 7 8 9 10
0 2 4 6 8 10 12 14 16 18 20
0 3 6 9 12 15 18 21 24 27 30
0 4 8 12 16 20 24 28 32 36 40
0 5 10 15 20 25 30 35 40 45 50
0 6 12 18 24 30 36 42 48 54 60
0 7 14 21 28 35 42 49 56 63 70
0 8 16 24 32 40 48 56 64 72 80
0 9 18 27 36 45 54 63 72 81 90
0 10 20 30 40 50 60 70 80 90 100

Do you know how can I modify the function in a way that I can get the first output? Thank you so much!

Please view this line!

print "*" * 10

And modify this as appropriate.

Edited 6 Years Ago by -ordi-: n/a

Definitely will do. I still need to learn a lot about Python. Thanks for the advice.

for x in range(5):
   print x,

#the same in C++ is:

for (int x = 0; x < 5; x++)
   cout << x;
//C++
#include <iostream>
using namespace std;
int main()

{
int size;
size = 4;

    for (int i = 0; i < size; i++)
        cout << '*' <<endl;
    return 0; 
}

#python
size = 4

for i in range(size):
    print '*'

#A python function for this
def asterix(num, lenght):
    a = '*' * num
    for i in range(lenght):
        print a

asterix(4,4)

'''-->Out
****
****
****
****
'''

The problem with Python when you come from C++ is that Python code is just mind-numbingly simple ...

def make_square(side):
    for k in range(side):
        print('*' * side)

make_square(4)

Edited 6 Years Ago by vegaseat: n/a

This article has been dead for over six months. Start a new discussion instead.