7 Replies - 1073 Views - Last Post: 20 April 2012 - 06:02 PM Rate Topic: -----

#1 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 04:45 PM

Hello, I am trying to write a python version of my Length of a Curve code (originally in C++ which now work just fine) but I am getting a syntax error that I don't understand. It highlights the ")" at the end of a line where I call a math.cos function, but I don't know what it wants me to do to fix it...it seems like there is the correct number of parentheses so any insight would be appreciated.

import math


def main():
    input(a,b,n,dx)
    sum=0
    for count in range(n):
        sum+=dist(a+(dx*count), a+(dx*(count+1)))
    print("Sum of lengths = ", sum)
        

def input(a,b,n,dx):
    a = float(input("Value for a: "))
    b = float(input("Value for b: "))
    n = float(input("Value for n: "))
    dx =  (b-a)/n

def f(x):
    return math.cos(x)

def dist(a,B)/>:
    d = (math.sqrt( (b-a)** + (f(B)/>-f(a))**) 
    return d

main()




Correction, at the end of the line where I call the math.sqrt function. OH, also how do I input several variables from the user in one line? In C++ I'm used to doing it like "cin >> a, b, n", in python i'm guessing it to be something like "a,b,n=input("string"),input("string"),input("string")?

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax Error in Length of Curve Prog in Python 3

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 04:53 PM

You're right that line 19 has the correct number of parentheses, but as you said, that's not the line with the error. Line 22 does not have the correct number of parentheses (there's one more opening than closing).

There's also something seriously wrong with the syntax of your expression on that line. You're using ** without a left operand (twice). ** is an infix operator that takes a left operand (the base) and a right operand (the exponent). In your code you only give a left operand both times you use it.

Quote

In C++ I'm used to doing it like "cin >> a, b, n", in python i'm guessing it to be something like "a,b,n=input("string"),input("string"),input("string")?


Yes, that'd work (or a,b,n = float(input("string")), float(input("string")), float(input("string")) if you want them to be floats). But that isn't really any more succinct than in writing it in three lines and makes for one long-ass line, so I wouldn't recommend doing that.

This post has been edited by sepp2k: 20 April 2012 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#3 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 05:14 PM

on line 5 I have the input() function called from within the main() function, and then I do stuff with those variables later in the main(), but it's not working out. Basically I'm trying to pass these variables by reference to the input() function, but not sure how to do this in python. in C++ I would do something like

input(double& a, double&b, int& n);

int main()
{
double a,b;
int n;
input(a,b,n)
}

so that the input() assigns user given values to the variables, which are then used later in the main(). how should I do this in python?


import math


def main():
    a,b,n,dx=0,0,0,0
    input(a,b,n,dx)
    sum=0
    for count in range(n):
        sum+=dist(a+(dx*count), a+(dx*(count+1)))
    print("Sum of lengths = ", sum)
        

def input(a,b,n,dx):
    a = float(input("Value for a: "))
    b = float(input("Value for b: "))
    n = float(input("Value for n: "))
    dx =  (b-a)/n

def f(x):
    return math.cos(x)

def dist(a,B)/>:
    d = (math.sqrt( (b-a)**2 + (f(B)/>-f(a))**2)) 
    return d

main()


so in the first couple lines of the main() function, i'm trying to declare these variables, then run input() on them (where a,b,n and inputs and dx is calculated) and then use those values later in main()
Was This Post Helpful? 0
  • +
  • -

#4 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 05:19 PM

Oh also in regards to the ** thing, I for some reason had it in my head that var** meant to square var, but now I realize that the exponent must be given. Do does this essentially make the pow() function obsolete?
Was This Post Helpful? 0
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 05:32 PM

View Postbennigan88, on 21 April 2012 - 02:19 AM, said:

does this essentially make the pow() function obsolete?


The pow function still has a three-argument version to efficiently calculate x to the power of y modulo z. But other than that I'm not aware of any reason to use pow over ** - except maybe if you want to pass pow as an argument to another function.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,311
  • Joined: 21-June 11

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 05:39 PM

View Postbennigan88, on 21 April 2012 - 02:14 AM, said:

on line 5 I have the input() function called from within the main() function, and then I do stuff with those variables later in the main(), but it's not working out. Basically I'm trying to pass these variables by reference to the input() function, but not sure how to do this in python.


You can't pass variables by reference in python. And in fact there's really no point in doing so here. You only ever assign your variables to 0, so you have something to pass to your input function, but you never use their initial values. So there's no benefit of setting the variables to 0 and then passing them by reference (which as I said you can't do anyway) over just returning them from your input function and assigning the result of the input function to your variables. I.e. instead of input(x,y,z) do x,y,z = input(). Note that functions can return more than one value in python.

You also should not name your input function input. The way you wrote it right now, it's infinitely recursive (actually it will crap out with an error because you're calling input with the wrong number of arguments).

This post has been edited by sepp2k: 20 April 2012 - 05:41 PM

Was This Post Helpful? 0
  • +
  • -

#7 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 05:52 PM

To your last paragraph, why shouldn't I call it input, and where is the infinite recursion? I'm sure you're right but I'd like to understand a little more


import math

def main():
    a,b,n,dx=input()
    sum=0
    for count in range(n):
        sum+=dist(a+(dx*count), a+(dx*(count+1)))
    print("Sum of lengths = ", sum)
        
def input():
    a = float(input("Value for a: "))
    b = float(input("Value for b: "))
    n = float(input("Value for n: "))
    dx =  (b-a)/n
    return a,b,n,dx

def f(x):
    return math.cos(x)

def dist(a,B)/>:
    d = (math.sqrt( (b-a)**2 + (f(B)/>-f(a))**2)) 
    return d

main()



So I have a,b,n,dx=input() and input() returns a,b,n,dx but I'm getting another error :/
Was This Post Helpful? 0
  • +
  • -

#8 bennigan88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 07-April 12

Re: Syntax Error in Length of Curve Prog in Python 3

Posted 20 April 2012 - 06:02 PM

Oh DUHH input is already in use. It's...oh god. Obviously...
http://i2.kym-cdn.com/entries/icons/square/000/006/482/template.jpg
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1