Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,199,290 members, 7,971,047 topics. Date: Wednesday, 09 October 2024 at 08:35 PM |
Nairaland Forum / Science/Technology / Programming / New To Python, Gurus Please Help Solve These Challenges. Thanks. (3964 Views)
Python Gurus In The House Help With This Questions / Python Gurus Please Help Me With This Code. / Nairaland Should Switch To Python/django. Opinion (2) (3) (4)
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" 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: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: 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: " 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: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: 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: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: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: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: 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: 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" return count |
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Nobody: 6:18am On Aug 13, 2018 |
Homguy:You know this could be done in 3 lines 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: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: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: Yh yh I understand... You'll see there are a lot of python functions that makes things easier 1 Like |
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Nobody: 10:39am On Aug 13, 2018 |
Homguy: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:thanks man. I am happy to be on this path . . 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: 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: 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: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:Pycharm bro |
Re: New To Python, Gurus Please Help Solve These Challenges. Thanks. by Homguy(m): 7:48am On Sep 06, 2018 |
dragnet:Alright thanks alot. I will do this. I thought there was a way to do it without multiple lines of codes. |
. / How To Be A Hacker / Urgent Help Needed On How To Save Fingerprint To Sql Using U Are U 4500 And Java
(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. 53 |