Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,074 members, 7,818,210 topics. Date: Sunday, 05 May 2024 at 10:24 AM

I Need HELP With This Python Problem - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / I Need HELP With This Python Problem (1503 Views)

Please Review This Python Blog By A Nigerian. / Delete / Help With This Python Algorithm.... (2) (3) (4)

(1) (Reply) (Go Down)

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:
#2. Repeat the greetings based on the number of character in 'greeting'

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:


Note: i am not a python programmer

For Repetition, your will need a loop

check the code below:


greetings = "HEELO GOOGLE"
numOfChars = len(greetings)

i = 0
while i < numOfChars:
print "Hello"
i = i + 1
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:
ignore first posting this is correct:
greetings = "HEELO GOOGLE"
numOfChars = greetings.split()
for char in numOfChars:
print greetings

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:
create a new list append greetings to it now you can index
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:
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.

Dude, smiley 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:


Dude, smiley 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.
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:


Dude, smiley 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.
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:

Dude, smiley your code worked. I'm still trying to decipher how you did it. Can you explain this part in bold a little for me:

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:


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)
Do you reply pm? I would like to contact you.

(1) (Reply)

Learn Any Course On Lynda And Udemy Today / Python Couldn't Install On My Laptop / I Built A Lpg Gas Station Sales Software Script With Php.

(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. 28
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.