So below is my first attempt at using user input, but I can't figure out how to get the input (at the bottom) to plug into my function definitions. I have no proper python training and am attempting to learn on my own with some books. I would appreciate any tips or suggestions.

Thanks!

#!/usr/bin/python

def dispatch(choice):
	if choice == 'A':
		functionA()
	elif choice == 'B':
		functionB()
	elif choice == 'C':
		functionC()
	else:
		print "An Invalid Choice"
		
def functionA():
	print "WRONG!"
def functionB():
	print "CORRECT!"
def functionC():
	print "WRONG!"
	
choice = raw_input ("A, B, or C\n")
print "Your answer is", choice

Edited 7 Years Ago by optikali: n/a

Try the appy(functionname, (args)) statement

def printit():
	print "hello, world!"

ch = input("enter a func->")

apply(ch, ())

Now at the prompt enter the function name printit and apply will execute it

Edited 7 Years Ago by gerard4143: n/a

Simply call the dispatch(choice) function with your choice as argument:

#!/usr/bin/python

def dispatch(choice):
    if choice == 'A':
        functionA()
    elif choice == 'B':
        functionB()
    elif choice == 'C':
        functionC()
    else:
        print "An Invalid Choice"
        
def functionA():
    print "WRONG!"
def functionB():
    print "CORRECT!"
def functionC():
    print "WRONG!"
    
choice = raw_input ("A, B, or C\n")
print "Your answer is", choice
# call the dispatch function with the argument in choice
dispatch(choice)

Simply call the dispatch(choice) function with your choice as argument:

#!/usr/bin/python

def dispatch(choice):
    if choice == 'A':
        functionA()
    elif choice == 'B':
        functionB()
    elif choice == 'C':
        functionC()
    else:
        print "An Invalid Choice"
        
def functionA():
    print "WRONG!"
def functionB():
    print "CORRECT!"
def functionC():
    print "WRONG!"
    
choice = raw_input ("A, B, or C\n")
print "Your answer is", choice
# call the dispatch function with the argument in choice
dispatch(choice)

Worked like a charm!

Quick question, how can I easily make it so input is not case sensitive?

Thanks again!

# now all input strings/characters are upper case
choice = raw_input ("A, B, or C\n").upper()

Edited 7 Years Ago by sneekula: n/a

So I added questions and tried to make some efficiency tweaks, but why is "B" not being seen as the correct answer (e.g. it triggers functionWrong)?

Note: if I split the " 'A' or 'C' " into two if conditions it works fine =/

Thanks

#!/usr/bin/python

def question():
	print "\n"
	print "What is the plural for Virus?\n"
	print "A. Virux"
	print "B. Viruses"
	print "C. Virus\n"
	choice = raw_input().upper()
	print "Your answer is", choice
	dispatch(choice)

def dispatch(choice):
	if choice == 'A' or 'C':
		functionWrong()
		question()
	elif choice == 'B':
		functionB()
		question()
	else:
		print "An Invalid Choice"
		question()
		
def functionWrong():
	print "WRONG!"

def functionB():
	print "CORRECT!"

question()

Edited 7 Years Ago by optikali: n/a

if choice == 'A' or 'C':
is wrong, has to be
if choice == 'A' or choice == 'C':
or simpler
if choice in 'AC':

It looks like you are using tabs for indentations, stay away from those and use four spaces instead. Indentations are too important in Python to waste them on tab troubles.

Edited 7 Years Ago by vegaseat: no tabs!

if choice == 'A' or 'C':
is wrong, has to be
if choice == 'A' or choice == 'C':
or simpler
if choice in 'AC':

It looks like you are using tabs for indentations, stay away from those and use four spaces instead. Indentations are too important in Python to waste them on tab troubles.

Thanks so much! Oh and it's good to know about tabbing, I should stop now before it becomes habit.

I think now that I'm getting these down my first big project will be a choose your own adventure game!

As you start coding larger projects with Python, remember to go in small steps, test each step, ask question here. Python can be fun!

Python is in many ways more advanced and modern than some of the old stale standby languages like Basic, C, C++, C# and Java. There is a lot of power under that hood, so drive responsibly.

This article has been dead for over six months. Start a new discussion instead.