Ask your own question, for FREE!
Computer Science 10 Online
OpenStudy (anonymous):

I'm doing problem set 3, problem 1 and I'm having some trouble. The problem asks you to write a program that will find x for a certain polynomial. Please see code: http://pastebin.com/A8mshUi7 When I run it, I get the following error: ValueError: invalid literal for int() with base 10: ',' I believe that means that it's trying to do raise the exponent to a tuple. But I thought I took care of that earlier in the code.... Thoughts?

OpenStudy (anonymous):

this seems to work - http://dpaste.com/773606/ something must be wrong with making the user's input into a tuple http://dpaste.com/773623/

OpenStudy (anonymous):

Yeah, the tuple function takes a string of characters and takes each one and puts it into a tuple. So tuple('1234') gives you ('1','2','3','4'). If you really want to take a string that looks like a tuple like '(1,2,3,4)' and turn it into the tuple (1,2,3,4), you can use the eval function instead, which takes a string and evaluates it as if it were typed into the interpreter not as a string.

OpenStudy (anonymous):

hmmm...haven't been taught that yet. I guess I'll have to look at the next lecture. Unless one of you can think of a different way?

OpenStudy (anonymous):

Actually, if you tell the user not to use parenthesis or spaces, this could potentially work I think. Where I use the variable 'n' in my output equation, I'll just adjust n by 2 instead of 1 after the calculation. Because if I subtract 1, then I get a comma. But if I subtract 2, I get the next character in the tuple. Let's see if it works!

OpenStudy (anonymous):

Yeah, right now your function is taking in two variables called poly and x, and is then using a raw_input function to get that data directly from the user. But there's no reason why you even need a poly function, because the data should have already been passed in when the function was called. For example, let's say I want to write a function that would take two numbers and add them together: def add_two(a,b): return a + b The way *you're* doing it would be like this. def add_two(a,b): a = int(raw_input("Enter a number")) b = int(raw_input("Enter another number")) return a + b You end up getting the same thing, but all the raw_input stuff is redundant. The user can just input the data using the function itself. add_two(4,5) ----> 9

OpenStudy (anonymous):

And, by the way, in the examples they give, there are no text input lines. The assignment wants you to just put input into the arguments and receive output.

OpenStudy (anonymous):

@shandelman I was aware of this, but I didn't know how the function would handle a tuple as an input. For a function that only had 2 inputs, each separated by a comma, how would it handle the introduction of x # of commas? And if you used parenthesis, how would it affect the tuple? Rather than go through that whole process, which would undoubtedly include debugging, I thought I'd do it the safe and redundant way. So yes, I appreciate your comment (really! and please keep them coming) but I found the need to defend my intelligence and methods.

OpenStudy (anonymous):

Sorry if it seemed like I was *insulting* your intelligence. I just assumed, if you were doing it that way, you didn't understand how function inputs work. I didn't think it was a stupid mistake. I think people don't know something until they do know something, if that makes sense. I'm not sure I *completely* understand your questions, but I'll try my best to answer them. Python is what is known as a lazily typed language, which means that when you declare that variables exist, it doesn't really care what they are until you assign values to them. So when you define evaluate_poly as having two inputs, poly and x, as soon as you treat poly as a tuple (which you're doing by asserting), then the computer will know that's what they are. The best reason for not using raw_input as the default to get values is that it makes the functions very hard to use in other functions. Imagine a case where you had a list of polynomials and you wanted to evaluate all of them. This could be done by walking through the list with a for loop and calling evaluate_poly each time. But if you use raw_input, then you can't do that because the user would have to manually enter in each polynomial.

OpenStudy (anonymous):

you can use anything in Python's arsenal to help you solve a problem - not just those that are exposed in the lectures.. http://dpaste.com/773726/

OpenStudy (anonymous):

I still think it's wrongheaded to be using a raw_input statement. *Especially* when you have to write the compute_root function which uses the evaluate_poly function. You're going to end up having nested raw_inputs which is never a good idea.

OpenStudy (anonymous):

@shandelman , point well taken. I adjusted my code accordingly. Not only is what you say true, but I found that when I entered a tuple when first declaring the function (as you suggested), there was no problem with commas being counted in the length. When I did so with raw_input, it treated commas as part of the tuple...if that makes any sense. Anyways, here my second crack at the code. No syntax errors, but I find that the code is not giving me proper answers and I was hoping someone could help me figure out why. http://pastebin.com/kGZr0cEu

OpenStudy (anonymous):

Would you believe the problem with your program is exactly one character? In your while loop, you have the line ans += ans + output. That should either be ans += output or ans = ans + output. Easy mistake to make, but also easily fixable! :)

OpenStudy (anonymous):

If you're walking through a list, consider using a for loop instead of a while loop... ans = 0 for n in range(len(poly)): ans = ans + poly[n]*x**n Your code is not wrong, but for loops are usually a bit better suited than while loops in this particular instance.

OpenStudy (anonymous):

the enumerate function is useful when you need to iterate a sequence and also need the index. http://dpaste.com/774626/

OpenStudy (anonymous):

@Shandelman, can you please explain your thinking on that? Why is the for loop better? I don't understand. For and While loops seem to do the same thing.

OpenStudy (anonymous):

@bwCA, we haven't been taught the enumerate function yet. While I understand that we can use anything to solve these equations, if I'm forced to use something that hasn't been taught, that means I currently don't understand what is BEING taught.

OpenStudy (anonymous):

With a while loop, you have to instantiate an iterating variable before the loop and then in the loop you have to increment (or decrement) the variable. For loops (at least in Python) take something that is meant to be iterated, like a list, and walk through it. The instantiation and iterating is done automatically. You're right that they both do the same thing. For loops just take less work on the programmer's part in certain cases.

OpenStudy (anonymous):

thanks for your response. can you please explain more? it now seems to me that for loops are always faster/easier. Yet you so that are like this only in certain cases.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!