Re: [Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop
Thanks, everyone, for your help. The objective was to extract all words from each line and place them in a list IF they didn't already exist in it. I sorted it out by adding little bits of everyone's suggestions. Here's the code that fixed it. fname = raw_input('Enter file name:\n') try: fhand = open(fname) except: print 'File cannot be found or opened:', fname exit() lst = list() for line in fhand: words = line.split() for word in words: if word in lst: continue else: lst.append(word) lst.sort() print lst I named the file WordExtract.py since it does just that. :) *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* On Thu, Jun 2, 2016 at 6:44 PM, Steven D'Aprano wrote: > On Thu, Jun 02, 2016 at 06:05:43PM +0100, Olaoluwa Thomas wrote: > > > fname = raw_input('Enter file name:\n') > > try: > > fhand = open(fname) > > except: > > print 'File cannot be found or opened:', fname > > exit() > > lst = list() > > for line in fhand: > > words = line.split() > > #print words (this was a test that a portion of my code was working) > > lst.append(words) > > If you printed words, you should have seen that it was a list. > > If you append a list to a list, what do you get? At the interactive > prompt, try it: > > > py> L = [1, 2, 3] > py> L.append([4, 5, 6]) > py> L > [1, 2, 3, [4, 5, 6]] > > > Append takes a single argument, and adds it *unchanged* to the end of > the list. > > What you want is the extend method. It takes a list as argument, and > appends each item individually: > > > py> L.extend([7, 8, 9]) > py> L > [1, 2, 3, [4, 5, 6], 7, 8, 9] > > > But if you didn't know about that, you could have done it the > old-fashioned way: > > lst = list() > for line in fhand: > words = line.split() > for word in words: > lst.append(word) > > > > -- > Steve > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Semantic Error: Trying to access elements of list and append to empty list with for loop
Hi Tutor, I'm trying to parse words in a file line by line and place all words into another list but I keep getting a list with nested lists. I would normally pore over it and go to google and fix my problems but this one escapes me and frankly, I'm tired of being stuck in the same place for almost a week. Here's the code: fname = raw_input('Enter file name:\n') try: fhand = open(fname) except: print 'File cannot be found or opened:', fname exit() lst = list() for line in fhand: words = line.split() #print words (this was a test that a portion of my code was working) lst.append(words) print lst A text file with the following contents "But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief" would give me the output in the attached screenshot [image: Inline image 2] whereas I want only one list containing strings not nested lists. Any help would be appreciated. *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issues converting a script to a functioin (or something) [SOLVED]
Gotcha. *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* On Sun, May 1, 2016 at 7:14 PM, Alan Gauld via Tutor wrote: > On 01/05/16 14:38, Olaoluwa Thomas wrote: > > > Thanks for your feedback. Please do not hesitate to provide more as I > shall > > email you personally in the future. > > Please don't do that. > a) Bob is a busy man who volunteers his time here, but may >have other things to do too. > b) The list is here so that everyone can benefit from the >discussions not only the people actively involved. > > > > The problem was that running the code gave an error which I now do not > > remember in detail as I have moved on after having fixed it. > > But the answer is nearly always in the detail. And as you get > more advanced in coding the errors get harder to spot. That's > why it is important to supply us (or any other forum) with > as much detail as you can. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issues converting a script to a functioin (or something) [SOLVED]
Hi Bob, Thanks for your feedback. Please do not hesitate to provide more as I shall email you personally in the future. The script is made up of a function definition and its call prompting the user for input. The script itself takes "number of hours worked" and "hourly rate" as inputs and gives gross pay as a product of the two. As I stated in my earlier email, there is also a portion for calculating gross pay with considerations for overtime (> 40 hours worked). The problem was that running the code gave an error which I now do not remember in detail as I have moved on after having fixed it. Here's the initial email below with Sreenathan's helpful input followed by my amendments to the code: (I thought my initial email was quite self-explanatory but what do I know... Please read through to the end) On Sun, May 1, 2016 at 1:09 PM, Sreenathan Nair wrote: > On Sun, May 01, 2016 at 5:34 PM, Olaoluwa Thomas > wrote: > > The novice Python programmer is back. > > I'm trying to incorporate a function and its call in the GrossPay.py > script > that Alan solved for me. > It computes total pay based on two inputs, no. of hours and hourly rate. > > There's a computation for overtime payments in the if statement. > > Something seems to be broken. > > Here's the code: > def computepay(hours, rate): > hours = float(raw_input ('How many hours do you work?\n')) > rate = float(raw_input ('What is your hourly rate?\n')) > if hours > 40: > gross = ((hours - 40) * (rate * 1.5)) + (40 * rate) > elif hours >= 0 and hours <= 40: > gross = hours * rate > print "Your Gross pay is "+str(round(gross, 4)) > > computepay() > > What am I doing wrong? > > *Warm regards,* > > *Olaoluwa O. Thomas,* > *+2347068392705* > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > Hi, > The parameters hours and rate are required when calling the method > computepay ex: computepay(8, 200), so basically computepay() by itself will > throw an error Also, as a suggestion if you're gonna get hours and > rate via user input perhaps they can be removed from the method definition? > > ​Thanks, Sreenathan. These alterations solved it. def computepay(hours, rate): if hours > 40: gross = ((hours - 40) * (rate * 1.5)) + (40 * rate) elif hours >= 0 and hours <= 40: gross = hours * rate print "Your Gross pay is "+str(round(gross, 4)) computepay(hours = float(raw_input ('How many hours do you work?\n')), rate = float(raw_input ('What is your hourly rate?\n'))) *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* On Sun, May 1, 2016 at 2:13 PM, Bob Gailer wrote: > > On May 1, 2016 8:04 AM, "Olaoluwa Thomas" > wrote: > > > > The novice Python programmer is back. > Welcome back. We are here to help you when you are stuck. Telling us > something is broken is not adequate. Tell us-what you are expecting the > program to do and what results you're getting. > > > > I'm trying to incorporate a function and its call in the GrossPay.py > script > > that Alan solved for me. > > It computes total pay based on two inputs, no. of hours and hourly rate. > > > > There's a computation for overtime payments in the if statement. > > > > Something seems to be broken. > > > > Here's the code: > > def computepay(hours, rate): > > hours = float(raw_input ('How many hours do you work?\n')) > > rate = float(raw_input ('What is your hourly rate?\n')) > > if hours > 40: > > gross = ((hours - 40) * (rate * 1.5)) + (40 * rate) > > elif hours >= 0 and hours <= 40: > > gross = hours * rate > > print "Your Gross pay is "+str(round(gross, 4)) > > > > computepay() > > > > What am I doing wrong? > > > > *Warm regards,* > > > > *Olaoluwa O. Thomas,* > > *+2347068392705* > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Issues converting a script to a functioin (or something)
The novice Python programmer is back. I'm trying to incorporate a function and its call in the GrossPay.py script that Alan solved for me. It computes total pay based on two inputs, no. of hours and hourly rate. There's a computation for overtime payments in the if statement. Something seems to be broken. Here's the code: def computepay(hours, rate): hours = float(raw_input ('How many hours do you work?\n')) rate = float(raw_input ('What is your hourly rate?\n')) if hours > 40: gross = ((hours - 40) * (rate * 1.5)) + (40 * rate) elif hours >= 0 and hours <= 40: gross = hours * rate print "Your Gross pay is "+str(round(gross, 4)) computepay() What am I doing wrong? *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issue with Code [SOLVED]
Thank you so much, Alan. That fixed it (See Script 2[SOLVED] below). For the purpose of record-keeping, I'm pasting the entire code of all scripts below as I should have done from the very beginning. P.S. How were you able to open attachments with the restrictions on this mailing list? Script 1 hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4)) Script 2 hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') if hours > 40: gross = ((float(hours) - 40) * (float(rate) * 1.5)) + (40 * float(rate)) elif hours >= 0 and hours <= 40: gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4)) Script 2 [SOLVED] hours = float(raw_input ('How many hours do you work?\n')) rate = float(raw_input ('What is your hourly rate?\n')) if hours > 40: gross = ((hours - 40) * (rate * 1.5)) + (40 * rate) elif hours >= 0 and hours <= 40: gross = hours * rate print "Your Gross pay is "+str(round(gross, 4)) I'm gonna add Try and Except to make it more responsive. Thanks a lot! *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* On Sun, May 1, 2016 at 2:00 AM, Alan Gauld via Tutor wrote: > On 01/05/16 01:16, Alan Gauld via Tutor wrote: > > > I can't see anything obviously wrong in your code > > I was too busy focusing on the calculations that > I didn't check the 'if' test closely enough. > You need to convert your values from strings > before comparing them. > > hours = float(raw_input ('How many hours do you work?\n')) > rate = float(raw_input ('What is your hourly rate?\n')) > if hours > 40: >gross = (hours-40)*(rate*1.5) + (rate*40) > else: >gross = hours*rate > > > Sorry, > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Fwd: Issue with Code
Hi All, I sent this forwarded email earlier but hadn't subscribed to the mailing list so I guess that's why I didn't get a response. Please review and advise. *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* -- Forwarded message -- From: Olaoluwa Thomas Date: Sat, Apr 30, 2016 at 4:30 PM Subject: Issue with Code To: tutor@python.org Hi, I'm new to Python and programming in general. I came across a simple exercise that is used to compute gross pay when prompted to enter number of hours and hourly rate. I've attached the scripts in question (created via Notepad++). The 1st script I wrote worked perfectly. The 2nd script makes amendments to the 1st by increasing the hourly rate by 50% when number of hours is greater than 40. The problem with this script is that the "else" statement (which is equivalent to the 1st script) does not compute gross pay accurately as seen in the attached screenshot. I would appreciate a logical explanation for why the "else" statement in the 2nd script isn't working properly. I'm running Python v2.7.8 on a Windows 7 Ultimate VM via Command prompt and my scripts are created and edited via Notepad++ v6.7.3 *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4))hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') if hours > 40: gross = ((float(hours) - 40) * (float(rate) * 1.5)) + (40 * float(rate)) else: gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4))___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Issue with Code
Hi, I'm new to Python and programming in general. I came across a simple exercise that is used to compute gross pay when prompted to enter number of hours and hourly rate. I've attached the scripts in question (created via Notepad++). The 1st script I wrote worked perfectly. The 2nd script makes amendments to the 1st by increasing the hourly rate by 50% when number of hours is greater than 40. The problem with this script is that the "else" statement (which is equivalent to the 1st script) does not compute gross pay accurately as seen in the attached screenshot. I would appreciate a logical explanation for why the "else" statement in the 2nd script isn't working properly. I'm running Python v2.7.8 on a Windows 7 Ultimate VM via Command prompt and my scripts are created and edited via Notepad++ v6.7.3 *Warm regards,* *Olaoluwa O. Thomas,* *+2347068392705* hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4))hours = raw_input ('How many hours do you work?\n') rate = raw_input ('What is your hourly rate?\n') if hours > 40: gross = ((float(hours) - 40) * (float(rate) * 1.5)) + (40 * float(rate)) else: gross = float(hours) * float(rate) print "Your Gross pay is "+str(round(gross, 4))___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor