• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Basic Python Help

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

Gabber359

Member
Joined
Feb 12, 2004
Location
The Netherlands
So, for college we have to learn some (basic) scripting using Python. Anyhow, this is supposed to be week 1 stuff but I can't get my head around it. I have zero idea how to do it. I've even got 'A Byte of Python' printed out, used google and other various resources but alas.

Here's the deal:

I have to write a program that creates the following:

xxxxx
x----x
xxxxx
(you can ignore the ---, but the forum wouldn't place the closing x in the right place so I had to draw it like this)

A rectangle. Length = equal to whatever Int you input. Height (or breadth) is always 3
I'm supposed to use the + and - operators aswell as \n

So far I have:

lengte = int(input("Geef de lengte op"))
a = 'x'
print(a*lengte)
print(a)
print(a*lengte)

This gives me:

xxxxx
x
xxxxx

Obviously I haven't used +. - or \n and no amount of math that I can think of would complete the rectangle. The college sheets we have are pretty useless aswell. I have another 83page python pdf with various examples, but none that cover this. I wish people would make a true beginners guide for this stuff. "Damnit Teacher I'm a network engineer, not a code junkie". Extremely frustrating.

Any help would be appreciated. Thanks.
 
Last edited:
One solution solution might be to use one or more for loops and the range function. I've included some links to tutorials below, but here's a quick example of how a for loop would work with range().

Code:
C:\Users\Bubba\Desktop>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win
Type "help", "copyright", "credits" or "license" for more information.
>>> start = 0
>>> num = 10
>>> for x in range(start, num):
...   print x
...
0
1
2
3
4
5
6
7
8
9

for loop tutorials:
https://wiki.python.org/moin/ForLoop
http://www.tutorialspoint.com/python/python_for_loop.htm

range():
https://docs.python.org/2/library/functions.html#range
 
The reason you're having trouble finding a mathematical way to do this is because it isn't a problem of math... at least, not in the sense you think.

You need to take your input parameters (length, height) and have your program interpret what those numbers actually mean in the context of what you're trying to do. In this example, length is how wide (ie, how many characters are on a line), and height is how tall (how many lines total). There are probably dozens of ways of solving this, some more graceful than others. pcarini pointed you in the right direction with for loops, that's the approach I would use as well. This problem doesn't really necessitate any arithmetic.
 
Back