[Python] Why is my syntax invalid?

  • Context: Python 
  • Thread starter Thread starter sk1105
  • Start date Start date
  • Tags Tags
    Python
Click For Summary
SUMMARY

The discussion centers on a syntax error in Python 2.7 code related to the incorrect use of the global keyword. The user attempted to declare global variables using the syntax "global m_mu = 106.0", which is invalid. The correct approach is to separate the declaration and assignment into two lines: first declaring the variable with "global m_mu" and then assigning it a value. This adjustment resolves the syntax error encountered in Enthought Canopy.

PREREQUISITES
  • Understanding of Python 2.7 syntax
  • Familiarity with global variable usage in Python
  • Basic knowledge of mathematical functions in Python, such as asin and sqrt
  • Experience with the Enthought Canopy IDE
NEXT STEPS
  • Review Python 2.7 documentation on variable scope and global variables
  • Learn about proper syntax for variable declarations in Python
  • Explore debugging techniques in Enthought Canopy
  • Investigate best practices for variable management in Python programming
USEFUL FOR

Python developers, especially those working with legacy code in Python 2.7, and anyone troubleshooting syntax errors in their code.

sk1105
Messages
88
Reaction score
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
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.
 

Similar threads

Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
10K