Reputation: 701
I was fooling around today and decided to create a program that prints out a beach, unfortunately, it doesn't work the way I want it to. Here is the source code for it:
def beach1():
for i in range(7):
global beach, water, land
(beach, water, land) = (5, 0 + 5, 35 - 5)
print(''.join(['o' for i in range(land)])+''.join(['*' for i in range(beach)])+''.join(['~' for i in range(water)]))
beach1()
Here is the current output:
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
oooooooooooooooooooooooooooooo*****~~~~~
And here is what I want it to output:
oooooooooooooooooooooooooooooo*****~~~~~
ooooooooooooooooooooooooo*****~~~~~~~~~~
oooooooooooooooooooo*****~~~~~~~~~~~~~~~
ooooooooooooooo*****~~~~~~~~~~~~~~~~~~~~
oooooooooo*****~~~~~~~~~~~~~~~~~~~~~~~~~
ooooo*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The reason I expect the output to change is because of the three variables, land
, beach
, and water
. Since these variables are global, they retain their value, so every time the for
loop repeats, land
's value will go down by 5. Therefore the land section of the output will be printed 5 less times every time the loop repeats. Vice versa for the water section of the output. When I came up with the idea in my head, it seemed like it would work, but now it doesn't. What am I doing wrong? All help is appreciated, thanks! Also, if you can come up with a better title, feel free to, I certainly can't.
Upvotes: 0
Views: 76
Reputation: 35247
you could also make your prints simpler…
>>> for i in range(1,8):
... print('%s%s%s' % ('o'*5*(7-i), '*'*5, '~'*5*(i)))
...
oooooooooooooooooooooooooooooo*****~~~~~
ooooooooooooooooooooooooo*****~~~~~~~~~~
oooooooooooooooooooo*****~~~~~~~~~~~~~~~
ooooooooooooooo*****~~~~~~~~~~~~~~~~~~~~
oooooooooo*****~~~~~~~~~~~~~~~~~~~~~~~~~
ooooo*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Upvotes: 2
Reputation: 20355
The problem is that you're setting the size of your 3 areas with this:
(beach, water, land) = (5, 0 + 5, 35 - 5)
Which is a constant value, so it won't change during the for
loop!
Juste make the values depend on i
, like this:
for i in range(8):
(beach, water, land) = (5, 5+5*i, 35-5*i)
Moreover, you are using the same variable, i
, for your main loop and your inner loops on line 5. You have to rename one of them!
Upvotes: 2