print "INSERT QUIZ INTRO" 
choices1();

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	001();

There's code past this but I imagine this is where it matters. When I run this, it says "NameError: name 'choices1' is not defined"

What can I do to fix this?

Dani AI

Generated

As and pointed out to , the immediate NameError came from calling a name before it exists. Python executes top-level code in order: a def binds a name when the interpreter reaches it, but the function body runs only when you call it. That means either define the function before you call it, or ensure the call can't happen until after all required names are bound (for example, call from another function or under a runtime check).

A few other things in the later snippet explain the "invalid syntax" errors and would bite you next:

  • Missing colons on if lines (e.g. if choice == 1:) raises SyntaxError. Parentheses around the condition are optional; the colon is mandatory.
  • Semicolons at ends of lines are unnecessary and usually just noise in Python (as noted).
  • Lines like "--------" by themselves are just string literals and do nothing; use print if you want output.
  • 001() is not a valid function name. In Python 2 001 is a numeric literal (octal) and 001() attempts to call an integer; in Python 3 leading zeros are a SyntaxError. Function names must start with a letter or underscore (e.g. q001() or question1()).

Input handling: in modern Python (3.x) input() returns a string, so convert it with int() and guard with try/except to catch non-numeric input. In legacy Python 2, input() evaluates the typed expression—use raw_input() and then int() to avoid surprises.

Example (Python 3 style):

try:
    choice = int(input("Enter choice number: "))
except ValueError:
    print("Please enter a valid integer.")

Quick checklist: move def before calls (or delay calls), add colons on control lines, pick valid function names, use proper input handling for your Python version, and watch indentation. The tutorial link suggested is a good next step for basics.

Recommended Answers

All 7 Replies

print "INSERT QUIZ INTRO" 
choices1();

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	001();

There's code past this but I imagine this is where it matters. When I run this, it says "choices1 not defined"

What can I do to fix this?

I think you should define the quote before u call it..

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	001();

print "INSERT QUIZ INTRO" 
choices1();
print "INSERT QUIZ INTRO" 
choices1()

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	001();

There's code past this but I imagine this is where it matters. When I run this, it says "choices1 not defined"

What can I do to fix this?

When you call choices1, it isn't yet defined. The solution is pretty obvious.

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	# 001(); This is guaranteed to raise an error on you.

print "INSERT QUIZ INTRO" 
choices1()

Btw, Python doesn't need semi-colons as line delimiters. Go read a tutorial.

Thanks! It worked! I didn't know it needed to be previously defined. Thank you!

While you're at it, could you tell me why line 7 is invalid syntax?

def loss():
	print "Game Over. Try again!"
	"--------"
	begin();
def input2():
	choice = input('Enter choice number:')
	if (choice == 1)
		loss();
	if (choice == 2)
		loss();
	if (choice == 3)
		choices3();
	if (choice == 4)
		loss();
	print "--------"

def choices2():
	print "What was the name of the Sharpclaw leader?"
	print "Andross"
	print "Sauria"
	print "General Scales"
	print "President Scales"
"--------"
def input1():
	choice = input('Enter choice number:');
	if (choice == 1):
		loss();
	if (choice == 2):
		choices2();
	if (choice == 3):
		loss();
	if (choice == 4):
		loss();
"--------"

def choices1():
	print "The SNES Star Fox games ran off of/was used to advertise what advancement?"
	print "1: 32 Bit color"
	print "2: Argonaut's Super FX Chip"
	print "3: Voice-like sound effects"
	print "4: Higher Frame rates"
	input1();
"--------"

print "Welcome to Pizearke's Star Fox quiz! Simply type the number of the correct choice after where it asks you to enter the choice number and press enter to play. There are ten questions. The quiz will restart if you get one wrong. Don't worry though, it's pretty easy. Try to get all the way through!"
print"--------" 
choices1();

Seems like you are transitioning from c++/c to python. ( BTW when you have an if statement, you don't have to have parenthesizes around your expression). When you call a function, there is no need to put a semicolon next to it. Taking it away should resolve the problem.

Seems like you are transitioning from c++/c to python. ( BTW when you have an if statement, you don't have to have parenthesizes around your expression). When you call a function, there is no need to put a semicolon next to it. Taking it away should resolve the problem.

Actually, I'm new to programming all together. Thanks, though!

Actually, I'm new to programming all together. Thanks, though!

From where are you learning your python then? I'd like to destroy whatever the source may be...

>While you're at it, could you tell me why line 7 is invalid syntax?
Line 7 would have made sense if you had followed the proper syntax of the code tags:
[code=python] // the code which goes here will have // line numbers at left side

[/code]

Anyways, it turns out that you don't even know the proper syntax of if statement. If was not a bad thing if you were migrating from another language, but the fact is that you are starting programming for the first time.

Anyways, this is the proper syntax:

if <condition> :    #notice the colon
        //do something when
        //the condition is true

>I'd like to destroy whatever the source may be...
I would be glad to help you.

PS: Please follow a definitive tutorial or book to study python. The good part is: that there are enormous resources available for you to learn and most of them are free. You could start learning by the official Python Tutorial or perhaps "" is also a valuable resource

commented: Doesn't the fact that he's starting programming for the first time make him more prone to make mistakes? -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.