I am quite new to the Python syntax in particular such as creating global, nonlocal and local variables and how to declare them without syntax errors .
For example in the next program, I have
# Estamos creando el juego de Piedra, papel o tijera
import random
userScore = 0;
cpuScore = 0;
global noChoice = True;
# result viene de que los jugadores han elegido, no muestro esta parte
# porque no es necesario por el tema de errores de sintaxis por las variables
def result(userChoice, cpuChoice):
# Traimos de saber lo que el usuario quiere elegir como estrategia
def start():
while (noChoice):
try:
userChoice = int(raw_input("Make a move: Rock = 1, Paper = 2, Scissors = 3"));
except ValueError: "That's definitely not a number"
if userChoice == {1,2,3}:
global noChoice = False;
print "Oops! That was no valid number. Try again..."
cpuChoice = random.getInt();
start()
This gives the following result:
:~$ python pFC.py
File "pFC.py", line 7
global noChoice = True;
^
SyntaxError: invalid syntax
I used global
but it says that I declared the variable too soon.
UnboundLocalError: local variable 'noChoice' referenced before assignment
I have used what I have been advised, but the error remains
# Estamos creando el juego de Piedra, papel o tijera
import random
user_score = 0
cpu_score = 0
global no_choice = True
# result viene de que los jugadores han elegido, no muestro esta parte
# porque no es necesario por el tema de errores de sintaxis por las variables
def result(user_choice, cpu_choice):
# Traimos de saber lo que el usuario quiere elegir como estrategia
def start():
#Hey Python! Vamos a utilizar una variable global!
global no_choice
while (no_choice):
try:
user_choice = int(raw_input("Make a move: Rock = 1, Paper = 2, Scissors = 3"))
except ValueError: "That's definitely not a number"
if user_choice == {1,2,3}:
global no_choice = False
print "Oops! That was no valid number. Try again..."
cpuChoice = random.getInt();
start()