Discussion:
[Tutor] Python type confusion?
Ken Hammer
2015-09-28 20:27:19 UTC
Permalink
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 - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
C Smith
2015-09-28 23:13:15 UTC
Permalink
Post by Ken Hammer
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?
y = int(x) #see below
Post by Ken Hammer
total = 0.0
totalTerm = poly[i]* (x ** i)
Here you would get "unsupported operand type" since you are getting
the ith power of a string.
Post by Ken Hammer
total += totalTerm
print "totalTerm ", i , totalTerm
print "Equation Value", total
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
C Smith
2015-09-28 23:13:58 UTC
Permalink
Post by C Smith
Post by Ken Hammer
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?
y = int(x) #see below
Post by Ken Hammer
total = 0.0
totalTerm = poly[i]* (x ** i)
Here you would get "unsupported operand type" since you are getting
the ith power of a string.
EDIT: change "x" to "y"
Post by C Smith
Post by Ken Hammer
total += totalTerm
print "totalTerm ", i , totalTerm
print "Equation Value", total
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
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-09-28 23:34:45 UTC
Permalink
Post by Ken Hammer
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.
As C Smith notes, raw_input() returns a string. As the name suggests,
it treats its input as raw text, and does not try to interpret it as
data.

Interpreting text as data isn't too bad if we follow certain
conventions. One of the most popular conventions is to treat the text
as JavaScript object notation (JSON), because basically everything
knows how to parse JSON these days. We can use the 'json' module to
parse JSON-encoded text.

For example, here's some sample use of the json.loads() string-parsing
function from the interactive interpreter:

#######################################
Post by Ken Hammer
import json
json.loads('[0.0, 0.0, 5.0, 9.3, 7.0]')
[0.0, 0.0, 5.0, 9.3, 7.0]
Post by Ken Hammer
json.loads('42')
42
#######################################

The main limitation here is that this knows how to handle lists, but
it doesn't know how to handle tuples, since there's no such thing as
tuples in JSON. Hopefully that isn't too painful for your case, but
let us know if it is.


To use this parser for your own program, add the import to the top of
your program:

#########
import json
#########

and then wrap a use of json.loads() around each of the raw_input()
calls. Like this:

##########################################
import json

poly = json.loads(raw_input("Type, 0.0-n ,"))
x = json.loads(raw_input("Type your val of x, "))

# ... rest of program here
##########################################


Hope this helps!
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Danny Yoo
2015-09-28 23:41:47 UTC
Permalink
Post by Danny Yoo
As C Smith notes, raw_input() returns a string. As the name suggests,
it treats its input as raw text, and does not try to interpret it as
data.
Whoops! I slightly misspoke here: I mean to say that it does not try
to interpret it as *structured* data.

That is, we want things that look like numbers to be treated as
numbers. Likewise, we'd like a string that looks like a list of
numbers to be treated as a list of numbers. That's the role of a
parser, and why we need to do something, such as using the
json.loads() function.

Others on the list might suggest instead using input() instead of
raw_input(), which will try to interpret what you enter in as if it
were a Python expression. Effectively, this will also parse the text
into structured values. However, although this is a straightforward
way to make your program work, I don't think it's a good approach
because input() has a hidden use of the infamous "eval()" function
embedded in it. Certain inputs to input() can cause your program to
do weird things due to its use of eval(), so I think it's best to
avoid it.

Dunno if you're familiar with all the problems with eval(); if you are
interested, ask, and I'm sure there will be a lot of response from the
list.

Best of wishes to you!
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-09-29 01:18:31 UTC
Permalink
On 28/09/2015 21:27, Ken Hammer wrote:

As you've all ready had answers I've just one thing to say below.
Post by Ken Hammer
total = 0.0
totalTerm = poly[i]* (x ** i)
total += totalTerm
print "totalTerm ", i , totalTerm
print "Equation Value", total
Perhaps the most used piece of code that although not actually wrong, is
certainly frowned upon as you can write:-

for i, item in enumerate(poly):
totalTerm = item * (x ** i)
...
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...