[Tutor] Python type confusion?
A simple "type" problem? The following code works as a py file with the XX'd lines replacing the two later "raw_input" lines. Why do the "raw_input" lines yield a TypeError: 'str' object is not callable? Same result if I use/omit the parens around the poly tuple. evaluate a polynomial as formula for a defined function ##poly = (0.0, 0.0, 5.0, 9.3, 7.0)# f(x) = 7x^4 + 9.3x^3 + 5x^2 ##x = -13 poly = raw_input("Type, 0.0-n ,")## these do not work in place of above x = raw_input("Type your val of x, ")## 'str' object is not callable? total = 0.0 for i in range(len(poly)): totalTerm = poly[i]* (x ** i) total += totalTerm print "totalTerm ", i , totalTerm print "Equation Value", total Good answer follows: totalTerm 0 0.0 totalTerm 1 0.0 totalTerm 2 845.0 totalTerm 3 -20432.1 totalTerm 4 199927.0 Equation Value 180339.9 >>> thanks, Ken Hammer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Run Python 2.7 on Android Tablet
My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. I'd like to study and explore away from my office and desk on my tablet running Android 4.2.2. Possible? thanks, Ken ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Modulus Operator ?
A text for Python 2.7 includes these two paragraphs. The first I understand. The 2nd below mystifies me. How do I use "x % 10" and "x % 100" in the context of the examples I've created following the paragraphs? "The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits." I understand these: x = 49/13 print x 3 y = 49%13 print y 10 z = 0.0 + x + y/13.0 print z 3.76923076923 print 49.0/13 3.76923076923 Thanks, Ken ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modulus Operator ? SOLVED
Thanks, Danny Got it. Ken In , on 11/20/15 at 01:39 PM, Danny Yoo said: >>>On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer wrote: >> >>>> y = 49%13 >>>> print y >>>> 10 >> >>>Actually, let me pretend for a moment that I don't know what the modulus >>>operator is. Why do we get 10 here? Can you verbalize the reason? >> >> 49 contains 13 3 times and leaves 10 to be divided. >> >> >>>Can you modify this example above to use the modulus operator with "10" on >>>the right hand side? What do you expect it computes? What do you see? >> >> I see these. Is this what you mean? In view of the third float entry I >> don't understand why the first two are showing me the digits of the >> dividend. Why doesn't y = 49%10 deliver 4 as an answer and with 100 as the >> divisor why don't I get 0? >> >>>>> y = 49%10 >>>>> print y >> 9 >>>>> y= 49%100 >>>>> print y >> 49 >>>>> 49/13.0 >> 3.7692307692307692 >Ok, I think I might see where're you getting stuck. >You're misinterpreting what you read about the modulus operator. Contrary >to what you've read, you don't use it a single time to get *all* the digits >of the dividend all at once. Rather, what it does do is let you probe at >the *very last* digit in the number. In that sense, when we're doing: >49 % 10 >What you're asking is what's left to be when we divide 49 by 10. >49 contains 10 4 times and leaves 9 to be divided. >and that's why 49 % 10 == 9: it gives us the last, rightmost digit of the >number. >So you might be feeling a little let down. What was it trying to say? > I think the text was trying to say, when it talks about getting all the >digits of a number, is that you can do *repeated* uses of division and >modulus can get the individual digits. >In a sense, division and modulus are like measuring tools that allow us to >shift and probe at an object to figure out what it's made of. To make this >concrete, imagine holding something underneath a microscope. For our >purposes, let's do this: > >thing = 123456 > >But close your eyes for a moment: pretend for a minute that you don't know >what this "thing" looks like. What can we do to inspect it, if we don't >want to look at it all at once? >We can probe and shift it, to inspect portions of it. ># >def probe(x): > """Return the last digit of x.""" > return x % 10 >def shift(x): >"""Move x a little to the right, dropping the last digit.""" >return x / 10 ># >Here, we'll have two functions that we'll use on our thing. But why do we >call these things "probe" and "shift"? >Because we can do things like this. We can get the last digit... > >>>> probe(thing) >6 > >And now we know that this thing ends with a 6. And we can move the thing >around and probe again, to get the second to last digit... > >>>> probe(shift(thing)) >5 > >So now we know this thing looks something like "...56" >And we can repeat. Shift the thing twice, and probe it to get the third to >the last digit... > >>>> probe(shift(shift(thing))) >4 > >... and so on. >Why is this useful? In general: there are times when we're dealing with >things that are REALLY large, things that we truly can't look at all at >once, and knowing how to deal with just things by progressively poking and >moving through it is a skill we can use to navigate through our data. >You'll hear the term "iteration", which is essentially what this is. >Exploring a number, using division and modulo operators, just happens to be >a really simple toy example of this in action. -- --- K.F.Hammer Associates Ken Hammer management consultations Saint Johnsbury, VT 05819 --- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 142, Issue 10
I have a more simple-minded solution. The error appears to occur in testing without revealing the problem by use of $1 base price in the test. Using $10,000 as the base price in the test reveals absurd numbers. I think the real problem lies in the inclusion of base price inappropriately, twice as in: >dealerPrep = basePrice + 500 >destinationCharge = basePrice + 250 "basePrice" has no place in those two lines. 500 and 250 alone express the definition of the challenge. Ken In , on 12/12/15 at 12:00 PM, tutor-requ...@python.org said: >Send Tutor mailing list submissions to > tutor@python.org >To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor >or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org >You can reach the person managing the list at > tutor-ow...@python.org >When replying, please edit your Subject line so it is more specific than >"Re: Contents of Tutor digest..." >Today's Topics: > 1. Calculation error with a simple program (Jim Gallaher) > 2. Re: Calculation error with a simple program (Alan Gauld) > 3. Re: Calculation error with a simple program (Laura Creighton) >-- >Message: 1 >Date: Sat, 12 Dec 2015 01:03:05 -0600 >From: Jim Gallaher >To: tutor@python.org >Subject: [Tutor] Calculation error with a simple program >Message-ID: <566bc6a9.6090...@gmail.com> >Content-Type: text/plain; charset=utf-8; format=flowed >Hi everyone. I'm reading through a beginners Python book and came up with >a super simple program. I'm not getting any errors and everything runs >through, but there's a logical calculation error. What the program does is >take an amount and calculate a couple percentages and add a couple fees. >For example, if I put in a value of 1, it will output 752.12 as the sub >total and 753.12 as the grand total. It's off by 1 on sub total and 2 on >grand total. >Thanks in advance! Jim Gallaher ># Car Salesman Calculator ># User enters the base price of the car and the program adds tax, license, >dealer prep, and destination charge. >print("Car Sales Calculator") >basePrice = int(input("Please enter in the price of the car: ")) ># Misc charges to be added to the total cost of the car >tax = basePrice * .07 >license = basePrice * .05 >dealerPrep = basePrice + 500 >destinationCharge = basePrice + 250 ># Add the total misc charges together >subTotal = float(tax + license + dealerPrep + destinationCharge) ># Add all the misic charges and include the base price >grandTotal = float(subTotal + basePrice) ># Display the results >print("\nThe sub total is", subTotal) >print("\nYour grand Total is", grandTotal) >input("\nPress the enter key to close the program.") >-- >Message: 2 >Date: Sat, 12 Dec 2015 08:04:52 + >From: Alan Gauld >To: tutor@python.org >Subject: Re: [Tutor] Calculation error with a simple program >Message-ID: >Content-Type: text/plain; charset=utf-8 >On 12/12/15 07:03, Jim Gallaher wrote: >> For example, if I put in a value of 1, it will output 752.12 as the sub >> total and 753.12 as the grand total. It's off by 1 on sub total and 2 on >> grand total. >Are you sure? Lets check the values... >> basePrice = int(input("Please enter in the price of the car: ")) >=> 1 >> tax = basePrice * .07 >=> 0.07 >> license = basePrice * .05 >=> 0.05 >> dealerPrep = basePrice + 500 >=> 501 >> destinationCharge = basePrice + 250 >=> 251 >> # Add the total misc charges together >> subTotal = float(tax + license + dealerPrep + destinationCharge) >=> 0.07 + 0.05 + 501 + 251 => 752.12 >> # Add all the misic charges and include the base price >> grandTotal = float(subTotal + basePrice) >=> 752.12 + 1 => 753.12 >Looks like Python got it right to me? -- --- K.F.Hammer Associates Ken Hammer management consultations Saint Johnsbury, VT 05819 --- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Why does loop duplicate?
Intent is to print "Jack, Kack, " with "O" and "Q" delivering a longer suffix. Instead, I get the printout shown with duplicates and a second deviation with "O" and "Q" as shown. Why? IDLE and Shell are Python 2.5.4 running on Windows 8.1. prefixes = 'JKLMNOPQ' ###FAILS WITH REPEATS suffix = 'ack' suffixb= 'uack' for letter in prefixes: if letter == "O": print letter + suffixb if letter == "Q": print letter + suffixb else: print letter + suffix >>> Jack Jack Kack Kack Lack Lack Mack Mack Nack Nack Ouack Oack Oack Pack Pack Quack Qack >>> Thanks, Ken Hammer ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor