Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,573 members, 7,955,114 topics. Date: Saturday, 21 September 2024 at 05:21 PM |
Nairaland Forum / Science/Technology / Programming / I Need HELP With This Python Problem (1546 Views)
Please Review This Python Blog By A Nigerian. / Delete / Help With This Python Algorithm.... (2) (3) (4)
I Need HELP With This Python Problem by Nobody: 7:22pm On Jun 11, 2015 |
#1. Find the number of characters stored in the variable - greeting (I've managed to solve this one). greeting = "Hello Google!" number_of_char = len(greeting) print number_of_char #2. Repeat the greetings based on the number of character in 'greeting' (THIS IS WHERE I need a little help.) greetings[0] ?? I'm not looking for someone to help me solve the problem, I only need clues on how to solve it. Thanks in advance. |
Re: I Need HELP With This Python Problem by codemarshal08(m): 8:11pm On Jun 11, 2015 |
handsomeness: Note: i am not a python programmer For Repetition, your will need a loop check the code below: greetings = "HEELO GOOGLE" numOfChars = len(greetings) # numOfChars = the number of characters in greetings i = 0 while i < numOfChars: print greetings # output greetings till i equals numOfChars i = i + 1 |
Re: I Need HELP With This Python Problem by Nobody: 8:27pm On Jun 11, 2015 |
codemarshal08:Hmm.. Thanks Codemarshal08. You were close. I'm actually only allowed to use python list. I'm still cranking my brain out here. Will post my version before Sun. |
Re: I Need HELP With This Python Problem by codemarshal08(m): 8:51pm On Jun 11, 2015 |
Oops, you mean you are not allowed to use loops ? |
Re: I Need HELP With This Python Problem by seunthomas: 7:36am On Jun 12, 2015 |
greetings = "HEELO GOOGLE" numOfChars = len(greetings) # numOfChars = the number of characters in greetings for char in numOfChars: print greetings # output greetings till i equals numOfChars |
Re: I Need HELP With This Python Problem by seunthomas: 7:38am On Jun 12, 2015 |
ignore first posting this is correct: greetings = "HEELO GOOGLE" numOfChars = greetings.split() for char in numOfChars: print greetings |
Re: I Need HELP With This Python Problem by Nobody: 1:31pm On Jun 12, 2015 |
create a new list append greetings to it now you can index |
Re: I Need HELP With This Python Problem by Greatfes17: 2:04pm On Jun 12, 2015 |
To solve the problem, you have to: 1. Figure a means to make python know the number of characters in the string, then use print statement to print it upto the number of characters in the string. This can be achieved using loop. The python 'while' and 'for' statements can do it perfectly. 2. Count the characters yourself and then write print statements upto the number of characters. This also works but is "never recommended". Hence dont use it. I attached a script to achieve this in python 3. Am just learning python so pls forgive me if my code does work for you. Although it worked fine in my device.
|
Re: I Need HELP With This Python Problem by Nobody: 4:48pm On Jun 12, 2015 |
seunthomas: Seun, I don't think your code worked for the problem. It only outputs 2 times. The len of the variable - greeting - is 13 (meaning they're 13 chars in greeting. This means that you're required to write a code that'll count the number of chars in the greeting variable and print hello world to match the number of chars in greeting). |
Re: I Need HELP With This Python Problem by Nobody: 4:51pm On Jun 12, 2015 |
cbtgeek:Please Sir, can you elaborate more on the list thingy. |
Re: I Need HELP With This Python Problem by Nobody: 4:59pm On Jun 12, 2015 |
Greatfes17: Dude, your code worked. I'm still trying to decipher how you did it. Can you explain this part in bold a little for me: greetings = "HELLO GOOGLE" numChars = len(greetings) for printgreetings in range(numChars): print(greetings) I will try to solve this problem in another way. |
Re: I Need HELP With This Python Problem by Greatfes17: 7:08pm On Jun 12, 2015 |
handsomeness:1. 'for' is python keyword used to iterate (do something) within its body repeatedly. 2. 'printgreatings' is a variable that will hold the condition needed to keep 'for' repeating. 3. 'in' is python keyword used to determine if a given object is in something. 4. 'range' is python function that will count from zero upto the given value minus one. In this case, python will use the 'for' to assign value to the variable 'printgreatings' from 0 upto the lenght of characters found in greatings minus one(1). In each assignment, python will execute the statement contained within the body of 'for'. It will repeat until the lenght of greatings is exhausted. You may not understand me clearly. But do understand that am still learning.[Happy learning] |
Re: I Need HELP With This Python Problem by Greatfes17: 7:17pm On Jun 12, 2015 |
handsomeness:The same result can be achieved using 'while' statement. |
Re: I Need HELP With This Python Problem by msmee(f): 3:53am On Jun 13, 2015 |
handsomeness: The part in bold is the for loop function. http://www.python-course.eu/python3_for_loop.php this would also work: greetings = "HELLO GOOGLE" for i in range(len(greetings)): print(greetings) |
Re: I Need HELP With This Python Problem by Greatfes17: 4:28pm On Jun 13, 2015 |
msmee:Do you reply pm? I would like to contact you. |
(1) (Reply)
How Does One Go From A Copy And Paste Programmer To Writing Your Own Code / Seasoned Full Stack App Developer Wanted For Partnership... / Blockchain Technology
(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. 31 |