| | |
First attempt at raw_input
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 0
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!
Thanks!
python Syntax (Toggle Plain Text)
#!/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
Last edited by optikali; Sep 18th, 2009 at 5:19 pm.
Try the appy(functionname, (args)) statement
Now at the prompt enter the function name printit and apply will execute it
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
Last edited by gerard4143; Sep 18th, 2009 at 5:32 pm.
Simply call the dispatch(choice) function with your choice as argument:
python Syntax (Toggle Plain Text)
#!/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)
No one died when Clinton lied.
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
Simply call the dispatch(choice) function with your choice as argument:
python Syntax (Toggle Plain Text)
#!/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!
python Syntax (Toggle Plain Text)
# now all input strings/characters are upper case choice = raw_input ("A, B, or C\n").upper()
Last edited by sneekula; Sep 18th, 2009 at 7:06 pm.
No one died when Clinton lied.
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 0
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
Note: if I split the " 'A' or 'C' " into two if conditions it works fine =/
Thanks
python Syntax (Toggle Plain Text)
#!/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()
Last edited by optikali; Sep 18th, 2009 at 8:57 pm.
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.
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.
Last edited by vegaseat; Sep 19th, 2009 at 12:26 am. Reason: no tabs!
May 'the Google' be with you!
•
•
Join Date: Sep 2009
Posts: 7
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
No one died when Clinton lied.
![]() |
Similar Threads
- While and raw_input (Python)
- Doubling the size of an image (Python)
- Posts fine but no attempt to boot (Troubleshooting Dead Machines)
- Possible Browser Hijack Attempt: What needs to be repaired? (Viruses, Spyware and other Nasties)
- I need some help with the following error code: "Attempt to determine... (Windows NT / 2000 / XP)
- My first attempt at ASP.NET... (ASP.NET)
Other Threads in the Python Forum
- Previous Thread: Recursion Benchmark
- Next Thread: Python help, accessing wikipedia pages
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color converter dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path phonebook port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse scrolledtext server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






