[Python] Why is my syntax invalid?

  • Thread starter sk1105
  • Start date
  • Tags
    Python
In summary, the conversation discusses the use of global variables in Python 2.7 and the potential syntax errors that may arise when declaring them. The speaker mentions changing the units of values assigned to variables and encountering difficulties with Enthought Canopy displaying the old values instead of the updated ones. They also acknowledge that global variables may not be the best solution and may explore alternative options in the future.
  • #1
sk1105
88
12
Python:
global m_mu = 106.0
global M_Z = 91200.0
global a = 1.0/128.0
global theta_w = asin(sqrt(0.23152))

global g_e = sqrt(4*pi*a)
global g_Z = g_e/(cos(theta_w)*sin(theta_w))

I have written this code in Python 2.7 using Enthought Canopy, and each line throws up a syntax error, but I have no idea why. Also, the values assigned to m_mu and M_Z used to be <<1 until I changed the units to make the numbers bigger, but when Enthought traces the line of code containing the syntax error, it gives me the old tiny value even though I had saved my changes. Does anyone know what is going on?

PS - I know global variables aren't entirely encouraged, but it seems to be the best solution for what I'm trying to do. Perhaps I'll find another way, but that's for a different discussion.
 
Technology news on Phys.org
  • #2
I think your problem is the syntax of the global keyword in Python. For whatever reason, the statement:

global x = 2

is not valid syntax. You need to specify it as:

global x
x = 2

This should fix your problem.
 

Related to [Python] Why is my syntax invalid?

1. Why am I getting a syntax error in my Python code?

A syntax error in Python means that there is something wrong with the structure or format of your code. This could be due to missing or misplaced punctuation, incorrect indentation, or using an invalid keyword.

2. How can I fix a syntax error in my Python code?

The best way to fix a syntax error is to carefully review your code and check for any mistakes in the syntax. You can also use a debugger or syntax checker tool to help identify and correct errors.

3. Can a single syntax error cause my entire code to not run?

Yes, a single syntax error can prevent your code from running properly. Python is an interpreted language, meaning it executes code line by line. If there is a syntax error in one line, the interpreter will stop and display an error message, preventing the rest of the code from running.

4. Why is it important to pay attention to syntax in Python?

Syntax is crucial in Python because it determines the structure and logic of your code. Without proper syntax, your code will not be able to run correctly. Additionally, following the standard syntax conventions makes your code more readable and easier to debug.

5. I can't find the syntax error in my code. What should I do?

If you are having trouble finding the syntax error in your code, try breaking it down into smaller sections and testing each section individually. You can also ask a colleague or consult online resources for help in identifying and fixing the error.

Similar threads

  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
21
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
10K
  • Programming and Computer Science
Replies
22
Views
5K
Back
Top