Re: [Tutor] int(1.99...99) = 1 and can = 2
On Sun, May 1, 2016 at 1:02 AM, boB Stepp wrote: > > py3: 1. > 2.0 > py3: 1.999 > 1.999 ... > It has been many years since I did problems in converting decimal to > binary representation (Shades of two's-complement!), but I am under > the (apparently mistaken!) impression that in these 0.999...999 > situations that the floating point representation should not go "up" > in value to the next integer representation. https://en.wikipedia.org/wiki/Double-precision_floating-point_format A binary64 float has 52 signifcand bits, with an implicit integer value of 1, so it's effectively 53 bits. That leaves 11 bits for the exponent, and 1 bit for the sign. The 11-bit exponent value is biased by 1023, i.e. 2**0 is stored as 1023. The minimum binary exponent is (1-1023) == -1022, and the maximum binary exponent is (2046-1023) == 1023. A biased exponent of 0 signifies either signed 0 (mantissa is zero) or a subnormal number (mantissa is nonzero). A biased exponent of 2047 signifies either signed infinity (mantissa is zero) or a non-number, i.e. NaN (mantissia is nonzero). The largest finite value has all 53 bits set: >>> sys.float_info.max 1.7976931348623157e+308 >>> sum(Decimal(2**-n) for n in range(53)) * 2**1023 Decimal('1.797693134862315708145274237E+308') The smallest finite value has the 52 fractional bits unset: >>> sys.float_info.min 2.2250738585072014e-308 >>> Decimal(2)**-1022 Decimal('2.225073858507201383090232717E-308') The machine epsilon value is 2**-52: >>> sys.float_info.epsilon 2.220446049250313e-16 >>> Decimal(2)**-52 Decimal('2.220446049250313080847263336E-16') Your number is just shy of 2, i.e. implicit 1 plus a 52-bit fractional value and a binary exponent of 0. >>> sum(Decimal(2**-n) for n in range(53)) Decimal('1.999777955395075') The next increment by epsilon jumps to 2.0. The 52-bit mantissa rolls over to all 0s, and the exponent increments by 1, i.e. (1 + 0.0) * 2**1. Python's float type has a hex() method to let you inspect this: >>> (1.9998).hex() '0x1.fp+0' >>> (2.0).hex() '0x1.0p+1' where the integer part is 0x1; the 52-bit mantissa is 13 hexadecimal digits; and the binary exponent comes after 'p'. You can also parse a float hex string using float.fromhex(): >>> float.fromhex('0x1.0p-1022') 2.2250738585072014e-308 >>> float.fromhex('0x1.fp+1023') 1.7976931348623157e+308 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Homework
I'm trying to do a python homework question and cannot figure out how to start at all. This is the question [image: pasted1] ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Homework
Katie Tuite writes: > I'm trying to do a python homework question and cannot figure out how > to start at all. You'll need to help us more than that :-) What is the confusion you have? What do you understand so far? Can you re-phrase the question in your words, so we can get some insight into what may be lacking in your understanding? > This is the question You'll need to write only plain text email (no attached documents, no “rich text”) for the information to survive correctly. This is always good practice for any technical discussion forum. -- \ “The best in us does not require the worst in us: Our love of | `\ other human beings does not need to be nurtured by delusion.” | _o__) —Sam Harris, at _Beyond Belief 2006_ | Ben Finney ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Issue with Code [SOLVED]
On 01/05/16 05:20, Olaoluwa Thomas wrote: > 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. > thanks :-) > P.S. How were you able to open attachments with the restrictions on this > mailing list? The two code attachments made it to my reader. But that seems to be a fairly arbitrary occurence. The screen shot didn't make it. Some make it, others don't. I don't know the exact set of rules that determine when an attachment gets through! > 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)) You could have left it as else rather than elif, but otherwise that's fine. > I'm gonna add Try and Except to make it more responsive. I'm not sure what you mean by responsive? The only place try/except could/should be applied is round the float conversions. But it only makes sense if you put them inside a loop so you can force the user to try again if the input is invalid. Something like: while True: try: value = float(input(...)) break except ValueError: print 'warning message' -- 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
Re: [Tutor] META: Moderation and subscription to the tutor list
On 01/05/16 05:18, Steven D'Aprano wrote: > What's your policy here on the tutor list? I don't really have a policy. The list policy, set by my predecessors, is to allow anyone to send mail and encourage them to subscribe. All unsubscribed mail goes to moderation (and there is not very much of it). New subscribers are automatically put on moderation. They are manually removed from moderation when they post often enough that I recognize their ID and have enough time/energy to visit the members page... Replies can be seen by non subscribers in several places including python.org, activestate.com and gmane. > I think we should require > subscription before people can post. That doesn't achieve much since several lists servers like gmane are subscribed so anyone on gmane etc can post (albeit they go into the moderation queue). And the hassle of subscribing may put some newbies off posting at all, which we don't want. > (And I think we should default to individual emails, > not daily digest.) Quite a lot of people use the digest service, especially lurkers. (A quick scan of the members lists suggests around 35-40% of all members use digest). I'd be reluctant to remove a service that is so widely used. While modern mail tools generally have filters that can do a similar job I do sympathise with digest users since I used to be one of them and it was a useful way to keep the mail count down. But they should take the time to post replies 'nicely'... -- 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
Re: [Tutor] META: Moderation and subscription to the tutor list
On 01/05/16 06:35, c...@zip.com.au wrote: > There seems to me a subjectly large number of very short threads with a > question from someone, a couple of responses from list members, and no > further > reply. > > To me this argues that either newcomers are not subscribed and probably do > not > see any responses, or that sufficient are discourteous enough or naive enough > to nothing bother to acknowledge help. I suspect the latter... -- 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
Re: [Tutor] META: Moderation and subscription to the tutor list
On 01/05/16 10:06, Alan Gauld via Tutor wrote: > Quite a lot of people use the digest service, especially lurkers. > (A quick scan of the members lists suggests around 35-40% > of all members use digest). I'd be reluctant to remove a > service that is so widely used. I've just had a look at the digest options and one possible option is to send a Mime format digest rather than plain text. I'm not sure what that would mean in practice but from experience on other lists it may mean users see individual messages that they can reply to. This would potentially avoid the long multi-message replies we currently see. I don't know how it would affect threading. I therefore propose to switch on MIME digest mode as a trial at the end of next week if I don't hear a compelling reason not to... Hopefully most modern mail tools can handle MIME digests nowadays. -- 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
Re: [Tutor] META: Moderation and subscription to the tutor list
On 01/05/16 07:23, boB Stepp wrote: > I am in agreement with this as well. I have often wondered if > newcomers are subscribed or not Most are. Several who are not, subscribe very soon after - presumably in response to the intro message. > as after subscription one receives a > very helpful email which addresses most of the common post formatting > issues that we seem to endlessly rehash. Or perhaps I am one of the > few who actually read it upon subscribing? Probably most don't read it (all). But many simply are not technically savvy enough to know how to post in plain text, or avoid top posting etc. There are foreign concepts to many of the modern generation of internet users. > I wonder no matter which way the current matter gets decided, if it > might be time to rewrite the automated response email. I'm open to suggestions on this. It has gradually grown over the years as new caveats get added. A rewrite is something that is within our remit and abilities without involving the list admins. > mentioned. It probably should be added. I feel that the interleaved > writing style employed by many lists is completely foreign to > newcomers to programming. Absolutely. In fact even some programmers have never come across it because it's not how most web forums (fora?) work. And business email is now almost universally on Outlook/Exchange and top posting is the norm. -- 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
Re: [Tutor] int(1.99...99) = 1 and can = 2
On Sun, May 01, 2016 at 01:02:50AM -0500, boB Stepp wrote: > Life has kept me from Python studies since March, but now I resume. > Playing around in the interpreter I tried: > > py3: 1. > 2.0 > py3: 1.999 > 1.999 Correct. Python floats carry 64 bits of value, which means in practice that they can carry about 16-17 significant figures in decimal. Starting with Python 2.6, floats have "hex" and "fromhex" methods which allow you to convert them to and from base 16, which is more compact than the base 2 used internally but otherwise equivalent. https://docs.python.org/2/library/stdtypes.html#float.hex So here is your second example, shown in hex so we can get a better idea of the internal details: py> (1.999).hex() '0x1.bp+0' The "p+0" at the end shows the exponent, as a power of 2. (It can't use "e" or "E" like decimal, because that would be confused with the hex digit "e".) You can see that the last hex digit is "b". If we add an extra digit to the end of the decimal 1.999, that final digit increases until we reach: py> (1.9997).hex() '0x1.fp+0' 1.9998 also gives us the same result. More on this later. If we increase the final decimal digit one more, we get: py> (1.).hex() '0x1.0p+1' which is equal to decimal 2: a mantissa of 1 in hex, an exponent of 1 in decimal, which gives 1*2**1 = 2. Given that we only have 64 bits for a float, and some of them are used for the exponent and the sign, it is invariable that conversions to and from decimal must be inexact. Remember that I mentioned that both 1.9997 and 1.9998 are treated as the same float? That is because a 64-bit binary float does not have enough binary decimal places to distinguish them. You would need more than 64 bits to tell them apart. And so, following the IEEE-754 standard (the best practice for floating point arithmetic), both numbers are rounded to the nearest possible float. Why the nearest possible float? Because any other choice, such as "always round down", or "always round up", or "round up on Tuesdays", will have *larger* rounding errors. Rounding errors are inescapable, but we can do what we can to keep them as small as possible. So, decimal strings like 1.999...97 generate the binary float with the smallest possible error. (In fact, the IEEE-754 standard requires that the rounding mode be user-configurable. Unfortunately, most C maths library do not provide that functionality, or if they do, it is not reliable.) A diagram might help make this more clear. This ASCII art is best viewed using a fixed-width font like Courier. Suppose we look at every single float between 1 and 2. Since they use a finite number of bits, there are a finite number of equally spaced floats between any two consecutive whole numbers. But because they are in binary, not decimal, they won't match up with decimal floats except for numbers like 0.5, 0.25 etc. So: 1 _ | _ | _ | _ | ... | _ | _ | _ 2 ---^^---^ ab c The first arrow ^ marked as "a" represents the true position of 1.999...97 and the second, "b", represents the true position of 1.999...98. Since they don't line up exactly with the binary float 0x1.ff, there is some rounding error, but it is the smallest error possible. The third arrow, marked as "c", represents 1.999...99. > py3: int(1.) > 2 > py3: int(1.999) > 1 The int() function always truncates. So in the first place, your float starts off as 2.0 (as seen above), and then int() truncates it to 2.0. The second case starts off as with a float 1.... which is '0x1.bp+0' which int() then truncates to 1. > It has been many years since I did problems in converting decimal to > binary representation (Shades of two's-complement!), but I am under > the (apparently mistaken!) impression that in these 0.999...999 > situations that the floating point representation should not go "up" > in value to the next integer representation. In ancient days, by which I mean the earlier than the 1980s, there was no agreement on how floats should be rounded by computer manufacturers. Consequently they all used their own rules, which contradicted the rules used by other manufacturers, and sometimes even their own. But in the early 80s, a consortium of companies including Apple, Intel and others got together and agreed on best practices (give or take a few compromises) for computer floating point maths. One of those is that the default rounding mode should be round to nearest, so as to minimize the errors. Otherwise, if you always round down, then errors accumulate faster. We can test this with the fractions and decimal modules: py> from fractions import Frac
[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] Issues converting a script to a functioin (or something)
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
Re: [Tutor] Python Homework
On May 1, 2016 4:33 AM, "Katie Tuite" wrote: > > I'm trying to do a python homework question and cannot figure out how to > start at all. > > This is the question > > [image: pasted1] As you can see attachments don't work. Include the code in your post. > ___ > 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] simple regex question
Hi. I have a chunk of text code, which has multiple lines. I'd like to do a regex, find a pattern, and in the line that matches the pattern, mod the line. Sounds simple. I've created a test regex. However, after spending time/google.. can't quite figure out how to then get the "complete" line containing the returned regex/pattern. Pretty sure this is simple, and i'm just missing something. my test "text" and regex are: s=''' ACCT2081''' pattern = re.compile(r'Course\S+|\S+\|') aa= pattern.search(s).group() print "sss" print aa so, once I get the group, I'd like to use the returned match to then get the complete line.. pointers/thoughts!! (no laughing!!) thanks guys.. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple regex question
bruce wrote: > Hi. I have a chunk of text code, which has multiple lines. > > I'd like to do a regex, find a pattern, and in the line that matches the > pattern, mod the line. Sounds simple. > > I've created a test regex. However, after spending time/google.. can't > quite figure out how to then get the "complete" line containing the > returned regex/pattern. > > Pretty sure this is simple, and i'm just missing something. > > my test "text" and regex are: > > > s=''' > id='CourseId10795788|ACCT2081|002_005_006' style="font-weight:bold;" > onclick='ShowSeats(this);return false;' alt="Click for Class Availability" > title="Click for Class Availability">ACCT2081''' > > > pattern = re.compile(r'Course\S+|\S+\|') > aa= pattern.search(s).group() > print "sss" > print aa > > so, once I get the group, I'd like to use the returned match to then get > the complete line.. > > pointers/thoughts!! (no laughing!!) Are you sure you are processing text rather than structured data? HTML doesn't have the notion of a "line". To extract information from HTML tools like Beautiful Soup are better suited than regular expressions: import bs4 import re s = ... soup = bs4.BeautifulSoup(s) for a in soup.find_all("a", id=re.compile(r"Course\S+\|\S+\|")): print a["id"] print a.text print a.parent.parent["colspan"] ___ 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]
You have two arguments in you function but when you call the function no argument is set in. Take the arguments out from the function if you want to use the the values from the user. Sent from my iPhone > On May 1, 2016, at 8:41 AM, Olaoluwa Thomas wrote: > > 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 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)
On Sun, May 01, 2016 at 5:34 PM, Olaoluwa Thomas < thomasolaol...@gmail.com [thomasolaol...@gmail.com] > 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? ___ 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)
On 01/05/16 12:55, Olaoluwa Thomas wrote: > It computes total pay based on two inputs, no. of hours and hourly rate. While you do specify two inputs you immediately throw them away and ask the user to provide the information. In general it is good practice to separate calculation from input/output operations. So in your case write the function to take the values provided by the user and return(not print) the result. Then when you call the function you pass in the values from the user and print the function output. This makes the function more likely to be reusable in other scenarios and easier to debug/test. def computePay(hours, rate): # an exercise for the reader... hours = float(raw_input ('How many hours do you work?\n')) rate = float(raw_input ('What is your hourly rate?\n')) print computPay(hours, rate) > Something seems to be broken. You must learn to provide more specific comments. Define what is broken. Do you get an error message? If so send it (all of it, not just a summary) If no error message what output did you get? What did you expect? Otherwise we wind up just guessing at what is going on. (And only a fool would run allegedly faulty code from an unknown source! :-) > 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() In this cae I'll guess it's the fact that you tell Python that computePay is a function that takes two arguments (hours, rate) but then call it with no arguments. But you should have got an error message saying something very like that? Here is what I get: >>> def f(x,y): pass ... >>> f() Traceback (most recent call last): File "", line 1, in TypeError: f() missing 2 required positional arguments: 'x' and 'y' >>> That's why including the error message helps so much... -- 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
Re: [Tutor] Issues converting a script to a functioin (or something) [SOLVED]
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
Re: [Tutor] simple regex question
On 01/05/16 17:49, bruce wrote: > Hi. I have a chunk of text code, which has multiple lines. > s=''' > id='CourseId10795788|ACCT2081|002_005_006' style="font-weight:bold;" > onclick='ShowSeats(this);return false;' alt="Click for Class Availability" > title="Click for Class Availability">ACCT2081''' That looks like HTML. regex won't work reliably on HTML. You would be better using an HTML parser like BeautifulSoup or even the standard library's html.parser(Python v3). That will give more precise and reliable results for only a little more effort. -- 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
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] "List" object is not callable
Thank you all for your responses. I am using Py 2.7 and this time I copied and pasted the code from here: http://www.opentechguides.com/how-to/article/python/57/python-ping-subnet.html to my system but received the same error when I ran it. You can see the error screenshot here: https://unsee.cc/sonezima/ Thank you. On Saturday, April 30, 2016 11:12 PM, Steven D'Aprano wrote: On Sat, Apr 30, 2016 at 06:51:17PM +, Jason N. via Tutor wrote: > Hello, > I found this simple script online but when I execute it I get the > following error: "TypeError: 'list' object is not callable" Here is > the code sample: > > import subprocess > ls_output= subprocess.check_output(['dir']) The code snippet works fine. Please check that the code you send is exactly the same as the code you are actually trying to run. Do not just retype the code from memory, copy and paste it. Also, please copy and paste the full traceback that you get, not just the final error message. Everything from the first "Traceback" line to the end. Finally, you should tell us what version of Python you are running, on what operating system (Linux, Mac OS, Windows XP, Windows 10, Android, something else), and whether you are using the standard Python interactive interpreter or something else (IDLE, iPython, Anaconda, PyCharm, etc.). -- 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
Re: [Tutor] int(1.99...99) = 1 and can = 2
On Sun, May 1, 2016 at 5:43 AM, Steven D'Aprano wrote: > On Sun, May 01, 2016 at 01:02:50AM -0500, boB Stepp wrote: >> Life has kept me from Python studies since March, but now I resume. >> Playing around in the interpreter I tried: >> >> py3: 1. >> 2.0 >> py3: 1.999 >> 1.999 [...] > Starting with Python 2.6, floats have "hex" and "fromhex" methods which > allow you to convert them to and from base 16, which is more compact > than the base 2 used internally but otherwise equivalent. > > https://docs.python.org/2/library/stdtypes.html#float.hex I had not read of these methods yet. Thanks! [...] > Given that we only have 64 bits for a float, and some of them are used > for the exponent and the sign, it is invariable that conversions to and > from decimal must be inexact. Remember that I mentioned that both > 1.9997 and 1.9998 are treated as the same float? > That is because a 64-bit binary float does not have enough binary > decimal places to distinguish them. You would need more than 64 bits to > tell them apart. And so, following the IEEE-754 standard (the best > practice for floating point arithmetic), both numbers are rounded to the > nearest possible float. Just before bed, I was looking at the Wikipedia article on IEEE-754. But it was not clear from it which of the "optional" rounding methods were being used by Python, though the behavior I was observing suggested rounding to nearest. [...] >> It has been many years since I did problems in converting decimal to >> binary representation (Shades of two's-complement!), but I am under >> the (apparently mistaken!) impression that in these 0.999...999 >> situations that the floating point representation should not go "up" >> in value to the next integer representation. > > In ancient days, by which I mean the earlier than the 1980s, ... Heavy sigh! I last explicitly did binary arithmetic in an Intro to C. Sc. class in 1975. So perhaps my incorrect expectation of *not* rounding up is an artifact of that long ago era. Thank you Steve, and Eryk, for your excellent explanations! I really appreciate the time and depth both of you put into your answers to my questions. boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] simple regex question
On 01/05/16 20:04, bruce wrote: > Hey all.. > > Yeah, the sample I'm dealing with is html.. I'm doing some "complex" > extraction, and i'm modifying the text to make it easier/more robust.. > > So, in this case, the ability to generate the line is what's needed > for the test.. > But as Peter explained HTML has no concept of a "line". Trying to extract a line from HTML depends totally on how the HTML is formatted by the author in the original file, but if you read it from a web server it may totally rearrange the content(while maintaining the HTML), thus breaking your code. Similarly if it gets sent via an email or some other mechanism. What you really want will be defined by the tags within which it lives. And that's what a parser does - finds tags and extracts the content. A regex can only do that for a very limited set of inputs. and it certainly can't guarantee a "line" of output. Even if it seems to work today it could fail completely next week even if the original HTML doesn't change. -- 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
Re: [Tutor] "List" object is not callable
Hello, I figured out the issue. It was a silly mistake - i was not running the correct code; instead was running code from another program which was active on a second tab. Thank you. On Sunday, May 1, 2016 4:15 PM, Jason N. via Tutor wrote: Thank you all for your responses. I am using Py 2.7 and this time I copied and pasted the code from here: http://www.opentechguides.com/how-to/article/python/57/python-ping-subnet.html to my system but received the same error when I ran it. You can see the error screenshot here: https://unsee.cc/sonezima/ Thank you. On Saturday, April 30, 2016 11:12 PM, Steven D'Aprano wrote: On Sat, Apr 30, 2016 at 06:51:17PM +, Jason N. via Tutor wrote: > Hello, > I found this simple script online but when I execute it I get the > following error: "TypeError: 'list' object is not callable" Here is > the code sample: > > import subprocess > ls_output= subprocess.check_output(['dir']) The code snippet works fine. Please check that the code you send is exactly the same as the code you are actually trying to run. Do not just retype the code from memory, copy and paste it. Also, please copy and paste the full traceback that you get, not just the final error message. Everything from the first "Traceback" line to the end. Finally, you should tell us what version of Python you are running, on what operating system (Linux, Mac OS, Windows XP, Windows 10, Android, something else), and whether you are using the standard Python interactive interpreter or something else (IDLE, iPython, Anaconda, PyCharm, etc.). -- 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 maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor