Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,439 members, 7,808,570 topics. Date: Thursday, 25 April 2024 at 01:33 PM

I Need Help With This Python Exercise - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I Need Help With This Python Exercise (1512 Views)

Help With This Python Algorithm.... / I Need HELP With This Python Problem / Please Help With This Python Code (2) (3) (4)

(1) (Reply) (Go Down)

I Need Help With This Python Exercise by Nobody: 12:28pm On Jul 18, 2012
It goes like this:
Write a program that asks users for their favourite colour. Create the following output (assuming "red" is the chosen colour) -




red red red red red red
red red red red
red
red
red
red
red red red red red red
red red red red
Re: I Need Help With This Python Exercise by Nobody: 12:32pm On Jul 18, 2012
I don't really want someone to code it for me. I want a person that will tell me how to go about it. Am still a python newbie.
Re: I Need Help With This Python Exercise by ektbear: 7:50am On Jul 19, 2012
what if they input blue

what should the output be?

Can you sketch the idea in pseudo-code, or whatever language you know how to use?
Re: I Need Help With This Python Exercise by Nobody: 3:42pm On Jul 19, 2012
ekt_bear: what if they input blue

what should the output be?

Can you sketch the idea in pseudo-code, or whatever language you know how to use?

lemme describe it the way i can-

a = 'red ' * 6
b = 'red '* 4
c ='red'

print(a)
print(b)
print(c)
print(c)
print(c)
print(c)
print(b)
print(a)


buh i freakin hate the output-
it can only output red. LOL.

Can i put it it inside a Function?
Re: I Need Help With This Python Exercise by ektbear: 8:47pm On Jul 19, 2012
You can use the range() function combined with a for loop (or list comprehension) to reduce the block of print(c) calls down to one line.

Probably also wise to wrap the above in a function. And have the color passed as a parameter to the function.

You can google how to do these things in python (range(), for loop, list comprehension, functions).
Re: I Need Help With This Python Exercise by Nobody: 10:20pm On Jul 19, 2012
yes, put it in a function and pass the color itself in as a parameter.
Re: I Need Help With This Python Exercise by Nobody: 5:36pm On Jul 20, 2012
favcolor = input('whats ur favorite color: ')


def saycolor()
a='favColor' * 6
b='favColor' * 4
c='favColor' * 1
print(a)
print(b)
print(c)
print(c)
print(c)
print(c)
print(b)
print(a)


color= ['blue','red','green','purple','yellow','indigo','sky blue','black','stick blue','grey','brown','white','silver']

saycolor()
Re: I Need Help With This Python Exercise by Nobody: 5:47pm On Jul 20, 2012
ekt_bear: You can use the range() function combined with a for loop (or list comprehension) to reduce the block of print(c) calls down to one line.

Probably also wise to wrap the above in a function. And have the color passed as a parameter to the function.

You can google how to do these things in python (range(), for loop, list comprehension, functions).
Sir,
I don't understand how 'range()' and 'for loop' function work.
I've read all i could about it.
But using list and function makes sense.

*still confused_i've been tryna make it work since morning.*
sir, please come to my aid.
Re: I Need Help With This Python Exercise by ektbear: 10:30pm On Jul 20, 2012
Here is a screenshot of my terminal:

Re: I Need Help With This Python Exercise by ektbear: 10:33pm On Jul 20, 2012
As you can see, calling the range() function returns a list.

For example, my call range(5) returns the list:

[0,1,2,3,4]

So, to perform a for loop in Python, you can do:

for x in range(5):
do_some_stuff()

In my case, do_some_stuff() is essentially just printing to the screen.

Makes sense?

I do agree with you though, Python list comprehensions seem way cooler to me. I prefer that style of programming to the for loop style in the above image.
Re: I Need Help With This Python Exercise by ektbear: 10:36pm On Jul 20, 2012
List comprension loops look like this:


>>> [print('Yay #{0}'.format(i)) for i in range(5)]
Yay #0
Yay #1
Yay #2
Yay #3
Yay #4


(This example doesn't seem to work in Python2 by the way, I get a syntax error. So use Python3).
Re: I Need Help With This Python Exercise by Nobody: 11:11pm On Jul 20, 2012
I had one major problem with my program.
I created a 'list of colors', lets say:

favcolor= input('What\'s your favorite color: ')




color = ['blue', 'brown', 'white', . . . . . . . . . . . . . .'green', 'black']

let say the user typed 'brown'. How can i loop through the list to get exactly what matches 'brown'- and how do i print it?
Can this work?
#it's an 'in' loop.

if favcolor in color:
print(favcolor)

#don't know whether that will work.

The syntax is:
if value in L:
print('some string', value).
Re: I Need Help With This Python Exercise by ektbear: 11:15pm On Jul 20, 2012
What do you want to happen if they enter a color that is not in your list?
Re: I Need Help With This Python Exercise by Nobody: 11:36pm On Jul 20, 2012
ekt_bear: What do you want to happen if they enter a color that is not in your list?
#store it in the list..and still prints it.

#i add/attach the newColor to the list.
if favColor in color:
False
color.append(favColor)
print('favcolor')

#what do you think?
Re: I Need Help With This Python Exercise by ektbear: 11:42pm On Jul 20, 2012
What is the point of this color list, if you'll print out whatever color they enter?


From your description so far, it sounds unnecessary. Just take the string they have, assume that it is a valid color, and call your function that prints it
Re: I Need Help With This Python Exercise by Nobody: 4:21am On Jul 21, 2012
ekt_bear: What is the point of this color list, if you'll print out whatever color they enter?


From your description so far, it sounds unnecessary. Just take the string they have, assume that it is a valid color, and call your function that prints it
hmmm..
That makes a lot of sense..list scrapped.
Let me just create a function that will print a BiG 'C' for me. And be able to call the function from anywhere in the script.

favcolor= input('What\'s your favorite Colour: ')

def sayColor():
a = str(favcolor) * 6
b = str(favcolor) * 4
c = str(favcolor) * 1
print(a)
print(b)
print(c)
print(c)
print(c)
print(c)
print(a)
print(b)

sayColor()
#how do you see it?
Re: I Need Help With This Python Exercise by ektbear: 5:06am On Jul 21, 2012
Hey,

I would suggest that you paste code on a site like pastebin.com which will not ruin your formatting. Then post a link to pastebin.com.
Re: I Need Help With This Python Exercise by Nobody: 6:28am On Jul 21, 2012
ekt_bear: Hey,

I would suggest that you paste code on a site like pastebin.com which will not ruin your formatting. Then post a link to pastebin.com.
This is the link:
http://pastebin.com/Eh7XsVev
Re: I Need Help With This Python Exercise by ektbear: 8:07am On Jul 21, 2012
Here is my version of the above:

http://pastebin.com/8MrHF7m3

Mostly minor tweaks of yours.
Re: I Need Help With This Python Exercise by Nobody: 9:49am On Jul 21, 2012
ekt_bear: oh, so the default python interpreter on my laptop is python2. And I guess I was modifying your code using that, rather than python3.

Anyways, you can patch this up for whatever version you are using (python3, i guess).

Actually, this illustrates a good point. Probably it is a good idea to write the dependencies a script has. I.e., if the script is python3 only, then make a comment about that at the top of the file.
hahahahaha.....
No wonder.. I'm through with this exercise. Thank you very much Sir..
God bless you- you've really helped me.
Re: I Need Help With This Python Exercise by lordZOUGA(m): 9:56am On Jul 21, 2012
mehn is python messed up or what?
Re: I Need Help With This Python Exercise by ektbear: 10:02am On Jul 21, 2012
oh, so the default python interpreter on my laptop is python2. And I guess I was modifying your code using that, rather than python3.

Anyways, you can patch this up for whatever version you are using (python3, i guess).

Actually, this illustrates a good point. Probably it is a good idea to write the dependencies a script has. I.e., if the script is python3 only, then make a comment about that at the top of the file.
Re: I Need Help With This Python Exercise by Nobody: 11:12am On Jul 21, 2012
handsomeness:
hahahahaha.....
No wonder.. I'm through with this exercise. Thank you very much Sir..
God bless you- you've really helped me.
Re: I Need Help With This Python Exercise by ektbear: 12:26pm On Jul 21, 2012
np
Re: I Need Help With This Python Exercise by Nobody: 2:20pm On Jul 21, 2012
some of us dont have the luxury of following links on pastebin
post it here, grab it and apply code syntax then grab all again and apply the quote

the purpose for the function is defeated here

def sayColor():
a = str(favcolor) * 6
b = str(favcolor) * 4
c = str(favcolor) * 1
print(a)
print(b)
print(c)
print(c)
print(c)
print(c)
print(a)
print(b)


change that to this, notice the bolded

def sayColor(clr):
a = str(clr) * 6
b = str(clr) * 4
c = str(clr) * 1
print(a)
print(b)
print(c)
print(c)
print(c)
print(c)
print(a)
print(b)

so you will call it like this

sayColor(favcolor)

when a child starts trying to walk, you take an interesting object, put it far away, then tell her to bring it
not that you actually want that object or have problem fetching it, you want her to walk more, thats what we trying to do below
you code is pretty, but lets do it some other way, so we can learn together.



#this function means we dont have to rewrite the logic when we need to output black color or purple or whatever color
#we will just call it like this sayColor(whatever_color)
def sayColor(clr):
#recursive function, yes, python lets you use functions inside a function and it's garbage collected at the end of it's parent suite
def childSC(the_color, nTimes, separator):
multiplyIt=(the_color * nTimes)+separator
#lets give the result back to whoever called childSC
return multiplyIt



# this is what breaks your output to the next line so we will pass this into our function as a separator
newline="\n"

#lets concatenate the functions, with the right parameters, Notice that here we multiplied the function by 4 when we needed the same thing 4 times
# |
# V
a = childSC(clr,6,newline) + childSC(clr,4,newline) + (childSC(clr,1,newline)*4) + childSC(clr,6,newline) + childSC(clr,4,newline)

print(a)

The reason for function's existence is to help pack up the logic in one box, so all you have to tell it is the color in vogue
how do you tell it? you "inject" the function with the new color like this

sayColor('red')
sayColor('blue')

or

favcolor= input('What\'s your favorite color: ')
sayColor(favcolor)

goodluck
Re: I Need Help With This Python Exercise by Nobody: 3:52pm On Jul 21, 2012
Hmmmmm...
Gosh, i love the way you shortened the whole code.
My code was way too big.
Thanks man.
Re: I Need Help With This Python Exercise by Nobody: 5:24pm On Jul 21, 2012
a 100 line code can be more efficient that a 10 line code that does same thing.
i see your approach as more efficient, someone else may think otherwise.
our logical reasoning differs, a bit

i wrote that so you can understand functions.
Re: I Need Help With This Python Exercise by Nobody: 8:06pm On Jul 21, 2012
webdezzi: a 100 line code can be more efficient that a 10 line code that does same thing.
i see your approach as more efficient, someone else may think otherwise.
our logical reasoning differs, a bit

i wrote that so you can understand functions.
hmmmmm....
Interesting..i thank God for gurus like you.

Can you use the range() function to do the exercise above?
Re: I Need Help With This Python Exercise by StarrMatthieu: 6:22am On Jul 22, 2012
BUMPS

(1) (Reply)

Android App Monetization Insights... / Nairalanders Programmers Please I Need Your Help / Learn Object Oriented Programming (OOP) AT SANDCROFT SOFTWARE

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 46
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.