Welcome, Guest: Join Nairaland / LOGIN! / Trending / Recent / New
Stats: 2,715,562 members, 6,418,410 topics. Date: Tuesday, 03 August 2021 at 06:02 AM

New To Python, Gurus Please Help Solve These Challenges. Thanks. - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / New To Python, Gurus Please Help Solve These Challenges. Thanks. (3306 Views)

Python Gurus Please Help Me With This Code. / Chemistry! Help A Brother To Solve These Questions / Java Gurus Please I Need This Little Assignment Solved ''appreciation Involved" (2) (3) (4)

(1) (2) (Reply) (Go Down)

New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:44pm On Jul 25, 2018
Hello brothers , i am learning python 3.x now, and i have been having some challenges with doing some issues. I am creating this thread to chronicle my challenges and sort for assistance from people vast in python scripting/programming.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:53pm On Jul 25, 2018
i was just playing around with codes after being on codecademy.com .
Goal: I want to write a script that takes a passage and returns the number of a particular word in that passage.

Now: i have written a piece of code that can give the number of a particular word(in this case ,"pray"wink in a list but not in a passage. how do i tweak this code to take in passages and return the numbers of an exact word in the passage.


this is the code as at now. Thanks

def pray_count(x):
count=0
for item in x:
if item =='pray':
count = 1+count
return count
dap = input('please input passage')
county=pray_count([dap])
print (county )
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 5:50pm On Jul 26, 2018
use the split function to split the passage then you can iterate it.

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 5:53pm On Jul 26, 2018
and here's a better statement for your count increment: count += 1
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 7:26pm On Jul 26, 2018
dragnet:
use the split function to split the passage then you can iterate it.
Thanks boss. i will try that.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by stancozy(m): 8:25pm On Jul 26, 2018
dragnet:
use the split function to split the passage then you can iterate it.

Draggi... howdy man. It’s been more than a decade
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:01pm On Jul 26, 2018
This is a tweaked version of the earlier code with input from dragnet. Thanks bro.

while True:
word = input("please input a word: "wink
def pray_count(x):
count=0
for item in x:
if item == word :
count += 1
return count

dap = input('please input passage: ')
#kay is a variable holping the output of the fx split()
kay=dap.split()
county=pray_count(kay)
print("There are %d instances of %s in this passage" % (county, word))
answer= input('Do you want to run another passage? Yes/No: ').lower()
if answer == 'yes':
continue
elif answer == 'no':
print('Good bye ')
break
else:
print('Please input a valid response')

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 10:31pm On Jul 26, 2018
stancozy:

Draggi... howdy man. It’s been more than a decade
yea, hope you doing good, it's been a long time.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 10:34pm On Jul 26, 2018
Homguy:
This is a tweaked version of the earlier code with input from dragnet. Thanks bro.

while True:
def pray_count(x):
count=0
for item in x:
if item =='pray':
count += 1
return count
dap = input('please input passage: ')
kay=dap.split()
county=pray_count(kay)
print (county)
answer= input('Do you want to run another passage? Yes/No: ').lower()
if answer == 'yes':
continue
elif answer == 'no':
print('Good bye ')
break
else:
print('Please input a valid response')


did this do exactly what you intended ?
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 12:02am On Jul 27, 2018
dragnet:


did this do exactly what you intended ?
i just made some new adjustments to the code i posted now and it works as good as i intend. Thanks boss.

i am also searching ways to get better at python and open to suggestions from python gurus in the house.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 2:15am On Jul 27, 2018
Homguy:
i just made some new adjustments to the code i posted now and it works as good as i intend. Thanks boss.

i am also searching ways to get better at python and open to suggestions from python gurus in the house.
That's nice, get the book "head first python", it's a good resource.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:17pm On Jul 28, 2018
dragnet:
That's nice, get the book "head first python", it's a good resource.
i will check it out. Thanks boss
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Conspiracist: 11:40pm On Jul 28, 2018
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 12:03am On Aug 08, 2018
hello guys this is a very short sample code wrote as a personal not long ago. while just playing with python. i am copying it here for the fun of it, and incase any newbie like myself wants to try. Ciao!
#this code takes in any number and returns the sum of its digits.

def digit_sum(n):
srt= str(n)
count=0
for num in srt:
count+= int(num)
return( count)
k = input('please input a number: ')

p= digit_sum(k)

print ("The sum of the digits in %s is %s" % (k, p))
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:54pm On Aug 10, 2018
This code is one that takes a passage and censors any words that you choose by replacing it with any other word you choose. This idea came from codecademy but codes we used to achieve the are different. This is my code construct, its way shorter than codes posed on the forum.
If here is a better way of doing this, please share.
Thanks.


Modified: I found out that this code is not as robust as it should be. if the word u want to censor exists as part of a larger word, it censors that larger word too.
For eample: ( 'There was a bit too much salt in the bitter bittle' ,censor= 'bit' ,replace= '*')
Result _ 'There was a *** too much salt in the ***ter ***bittle'.

def censor(text,word):
return text.replace(word,rep)

texte=input('please input your passage: ')
worde=input('please input forbidden word: ')
rep=input('please input the new word: ')
k=censor(texte,worde)
print(k)

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Red2C(m): 4:34pm On Aug 12, 2018
Try using regex
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Snwokenk: 12:57am On Aug 13, 2018
Homguy:
i was just playing around with codes after being on codecademy.com .
Goal: I want to write a script that takes a passage and returns the number of a particular word in that passage.

Now: i have written a piece of code that can give the number of a particular word(in this case ,"pray"wink in a list but not in a passage. how do i tweak this code to take in passages and return the numbers of an exact word in the passage.


this is the code as at now. Thanks

def pray_count(x):
count=0
for item in x:
if item =='pray':
count = 1+count
return count
dap = input('please input passage')
county=pray_count([dap])
print (county )

This is too much. A more pythonic way can be using the built-in function count() for list or string

dap = input("please input passage" )
print(dap.count("pray" ))

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Snwokenk: 1:08am On Aug 13, 2018
Homguy:
hello guys this is a very short sample code wrote as a personal not long ago. while just playing with python. i am copying it here for the fun of it, and incase any newbie like myself wants to try. Ciao!
#this code takes in any number and returns the sum of its digits.

def digit_sum(n):
srt= str(n)
count=0
for num in srt:
count+= int(num)
return( count)
k = input('please input a number: ')

p= digit_sum(k)

print ("The sum of the digits in %s is %s" % (k, p))

What happens if someone puts a non numeric character into the string.
the line:
srt = str(n)
is not necessary since k is already a string.
When accepting input from user/internet always error check!

something like:
def digit_sum(n):
srt= str(n)
count=0
for num in srt:
try:
count+= int(num)
except ValueError:
pass # this can even be a print ie print('{} is not a number'.format(num))
# or using the new f string in 3.6+ print(f"{num} is not a number"wink
return count
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Nobody: 6:18am On Aug 13, 2018
Homguy:
i was just playing around with codes after being on codecademy.com .
Goal: I want to write a script that takes a passage and returns the number of a particular word in that passage.

Now: i have written a piece of code that can give the number of a particular word(in this case ,"pray"wink in a list but not in a passage. how do i tweak this code to take in passages and return the numbers of an exact word in the passage.


this is the code as at now. Thanks

def pray_count(x):
count=0
for item in x:
if item =='pray':
count = 1+count
return count
dap = input('please input passage')
county=pray_count([dap])
print (county )
You know this could be done in 3 lines shocked

passage = input('Enter the passage:\n')
word = input('\nThe word to be counted:\n')
print('\n{} appears {} time/times.'.format(word, passage.count(word)))

You should learn about built in string functions in Python.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:37am On Aug 13, 2018
Snwokenk:


What happens if someone puts a non numeric character into the string.
the line:
srt = str(n)
is not necessary since k is already a string.
When accepting input from user/internet always error check!

something like:
def digit_sum(n):
srt= str(n)
count=0
for num in srt:
try:
count+= int(num)
except ValueError:
pass # this can even be a print ie print('{} is not a number'.format(num))
# or using the new f string in 3.6+ print(f"{num} is not a number"wink
return count
Wow! I didnt know I could test inputs without "if" conditionals. So I assume that "try " statement attempts the action, and if there is an error runs the line under ValueError. I will try this in my next code.

Thanks boss.
Btw, what part of python applications is most intresting and sellable in your opinion. Web apps? Gui programming?

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 9:48am On Aug 13, 2018
Darivie04:

You know this could be done in 3 lines shocked

passage = input('Enter the passage:\n')
word = input('\nThe word to be counted:\n')
print('\n{} appears {} time/times.'.format(word, passage.count(word)))

You should learn about built in string functions in Python.
Thanks for this. I believe I may not be able to grasp all built-in functions in python and what they do except by practise ,time, experience and guidance like you have just provided. I recall I have checked pycharm's library of inbuilt functions but cannot remember coming across count().
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Nobody: 10:25am On Aug 13, 2018
Homguy:
Thanks for this. I believe I may not be able to grasp all built-in functions in python and what they do except by practise ,time, experience and guidance like you have just provided. I recall I have checked pycharm's library of inbuilt functions but cannot remember coming across count().

Yh yh I understand...
You'll see there are a lot of python functions that makes things easier grin

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Nobody: 10:39am On Aug 13, 2018
Homguy:
Wow! I didnt know I could test inputs without "if" conditionals. So I assume that "try " statement attempts the action, and if there is an error runs the line under ValueError. I will try this in my next code.

Thanks boss.
Btw, what part of python applications is most intresting and sellable in your opinion. Web apps? Gui programming?
The fields Python is currently thriving in are Web apps, machine learning and data mining

2 Likes

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 11:05am On Aug 13, 2018
Darivie04:

The fields Python is currently thriving in are Web apps, machine learning and data mining
thanks man. I am happy to be on this path grin grin grin. . I really love these applications

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Snwokenk: 5:37pm On Aug 13, 2018
Homguy:
Wow! I didnt know I could test inputs without "if" conditionals. So I assume that "try " statement attempts the action, and if there is an error runs the line under ValueError. I will try this in my next code.

Thanks boss.
Btw, what part of python applications is most intresting and sellable in your opinion. Web apps? Gui programming?

You're welcome,
web apps (django framework, flask) are the way to go.

Here in the US, using python for data science, machine learning and AI is also very popular.

1 Like

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by crunchyDope(m): 6:46pm On Aug 14, 2018
Homguy:
hello guys this is a very short sample code wrote as a personal not long ago. while just playing with python. i am copying it here for the fun of it, and incase any newbie like myself wants to try. Ciao!
#this code takes in any number and returns the sum of its digits.

def digit_sum(n):
srt= str(n)
count=0
for num in srt:
count+= int(num)
return( count)
k = input('please input a number: ')

p= digit_sum(k)

print ("The sum of the digits in %s is %s" % (k, p))

error handling is missing
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Dhareey(m): 3:05pm On Aug 15, 2018
Hello guys, I have a pandas datareader problem. I try getting some data from google finance with pandas data_reader but it kept giving me UnicodeDecodeError. Attached is a picture of a code and error. Will love if anyone can help me with it

Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 11:31pm On Sep 05, 2018
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by TheManofTomorrow(m): 1:31am On Sep 06, 2018
Homguy, what editors do you use in writing ur codes?
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by dragnet: 7:19am On Sep 06, 2018
Homguy:
hello friends,
please, how can I solve this puzzle?
I want to break a string with several letters and no spaces into a list. what method can I call on it?
for example:
card = 'AGDTDGATDGATDGATGDTGADGATDGATDGTAGATDGATDGATDGADTGADGTADGTAGDTADGTA'
how can I break this into a list with strings of 3 chars each? A list like ['AGD', 'TDG', 'ATD'...].


What I have done;
I have used the split() method but it does not work nor give the desired result, as it requires a delimiter and there's no sort of delimiter in this random code.
cc;
Snwokenk
Darivie04
dragnet
I don't know of any BIF that you can use, you may need to convert to a list, and do some iterations while using pop,then append what you collect to a new list.
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 7:42am On Sep 06, 2018
TheManofTomorrow:
Homguy, what editors do you use in writing ur codes?
Pycharm bro
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 7:48am On Sep 06, 2018
dragnet:

I don't know of any BIF that you can use, you may need to convert to a list, and do some iterations while using pop,then append what you collect to a new list.
Alright thanks alot. I will do this. I thought there was a way to do it without multiple lines of codes.

(1) (2) (Reply)

Be Sincere, Is Programming That Lucrative? / Where Are The Mods? / Where Can I Get A Free Software That Does This?

(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 - 2021 Oluwaseun Osewa. All rights reserved. See How To Advertise. 136
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.