First attempt at raw_input

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 7
Reputation: optikali is an unknown quantity at this point 
Solved Threads: 0
optikali optikali is offline Offline
Newbie Poster

First attempt at raw_input

 
0
  #1
Sep 18th, 2009
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!

  1. #!/usr/bin/python
  2.  
  3. def dispatch(choice):
  4. if choice == 'A':
  5. functionA()
  6. elif choice == 'B':
  7. functionB()
  8. elif choice == 'C':
  9. functionC()
  10. else:
  11. print "An Invalid Choice"
  12.  
  13. def functionA():
  14. print "WRONG!"
  15. def functionB():
  16. print "CORRECT!"
  17. def functionC():
  18. print "WRONG!"
  19.  
  20. choice = raw_input ("A, B, or C\n")
  21. print "Your answer is", choice
Last edited by optikali; Sep 18th, 2009 at 5:19 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 411
Reputation: gerard4143 is on a distinguished road 
Solved Threads: 48
gerard4143's Avatar
gerard4143 gerard4143 is offline Offline
Posting Pro in Training

Re: First attempt at raw_input

 
0
  #2
Sep 18th, 2009
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
Last edited by gerard4143; Sep 18th, 2009 at 5:32 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: First attempt at raw_input

 
0
  #3
Sep 18th, 2009
Simply call the dispatch(choice) function with your choice as argument:
  1. #!/usr/bin/python
  2.  
  3. def dispatch(choice):
  4. if choice == 'A':
  5. functionA()
  6. elif choice == 'B':
  7. functionB()
  8. elif choice == 'C':
  9. functionC()
  10. else:
  11. print "An Invalid Choice"
  12.  
  13. def functionA():
  14. print "WRONG!"
  15. def functionB():
  16. print "CORRECT!"
  17. def functionC():
  18. print "WRONG!"
  19.  
  20. choice = raw_input ("A, B, or C\n")
  21. print "Your answer is", choice
  22. # call the dispatch function with the argument in choice
  23. dispatch(choice)
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: optikali is an unknown quantity at this point 
Solved Threads: 0
optikali optikali is offline Offline
Newbie Poster

Re: First attempt at raw_input

 
0
  #4
Sep 18th, 2009
Originally Posted by sneekula View Post
Simply call the dispatch(choice) function with your choice as argument:
  1. #!/usr/bin/python
  2.  
  3. def dispatch(choice):
  4. if choice == 'A':
  5. functionA()
  6. elif choice == 'B':
  7. functionB()
  8. elif choice == 'C':
  9. functionC()
  10. else:
  11. print "An Invalid Choice"
  12.  
  13. def functionA():
  14. print "WRONG!"
  15. def functionB():
  16. print "CORRECT!"
  17. def functionC():
  18. print "WRONG!"
  19.  
  20. choice = raw_input ("A, B, or C\n")
  21. print "Your answer is", choice
  22. # call the dispatch function with the argument in choice
  23. dispatch(choice)

Worked like a charm!

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

Thanks again!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: First attempt at raw_input

 
0
  #5
Sep 18th, 2009
  1. # now all input strings/characters are upper case
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: optikali is an unknown quantity at this point 
Solved Threads: 0
optikali optikali is offline Offline
Newbie Poster

Re: First attempt at raw_input

 
0
  #6
Sep 18th, 2009
You Rock! =D
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: optikali is an unknown quantity at this point 
Solved Threads: 0
optikali optikali is offline Offline
Newbie Poster

Re: First attempt at raw_input

 
0
  #7
Sep 18th, 2009
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

  1. #!/usr/bin/python
  2.  
  3. def question():
  4. print "\n"
  5. print "What is the plural for Virus?\n"
  6. print "A. Virux"
  7. print "B. Viruses"
  8. print "C. Virus\n"
  9. choice = raw_input().upper()
  10. print "Your answer is", choice
  11. dispatch(choice)
  12.  
  13. def dispatch(choice):
  14. if choice == 'A' or 'C':
  15. functionWrong()
  16. question()
  17. elif choice == 'B':
  18. functionB()
  19. question()
  20. else:
  21. print "An Invalid Choice"
  22. question()
  23.  
  24. def functionWrong():
  25. print "WRONG!"
  26.  
  27. def functionB():
  28. print "CORRECT!"
  29.  
  30. question()
Last edited by optikali; Sep 18th, 2009 at 8:57 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: First attempt at raw_input

 
0
  #8
Sep 19th, 2009
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.
Last edited by vegaseat; Sep 19th, 2009 at 12:26 am. Reason: no tabs!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 7
Reputation: optikali is an unknown quantity at this point 
Solved Threads: 0
optikali optikali is offline Offline
Newbie Poster

Re: First attempt at raw_input

 
0
  #9
Sep 19th, 2009
Originally Posted by vegaseat View Post
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,297
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: First attempt at raw_input

 
0
  #10
Sep 19th, 2009
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.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC