943,520 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1061
  • Python RSS
Sep 18th, 2009
0

First attempt at raw_input

Expand Post »
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!

python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optikali is offline Offline
13 posts
since Sep 2009
Sep 18th, 2009
0

Re: First attempt at raw_input

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.
Reputation Points: 499
Solved Threads: 366
Postaholic
gerard4143 is offline Offline
2,195 posts
since Jan 2008
Sep 18th, 2009
0

Re: First attempt at raw_input

Simply call the dispatch(choice) function with your choice as argument:
python Syntax (Toggle Plain Text)
  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)
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 18th, 2009
0

Re: First attempt at raw_input

Click to Expand / Collapse  Quote originally posted by sneekula ...
Simply call the dispatch(choice) function with your choice as argument:
python Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optikali is offline Offline
13 posts
since Sep 2009
Sep 18th, 2009
0

Re: First attempt at raw_input

python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Sep 18th, 2009
0

Re: First attempt at raw_input

You Rock! =D
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optikali is offline Offline
13 posts
since Sep 2009
Sep 18th, 2009
0

Re: First attempt at raw_input

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

python Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optikali is offline Offline
13 posts
since Sep 2009
Sep 19th, 2009
0

Re: First attempt at raw_input

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!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is online now Online
5,792 posts
since Oct 2004
Sep 19th, 2009
0

Re: First attempt at raw_input

Click to Expand / Collapse  Quote originally posted by vegaseat ...
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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
optikali is offline Offline
13 posts
since Sep 2009
Sep 19th, 2009
0

Re: First attempt at raw_input

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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Recursion Benchmark
Next Thread in Python Forum Timeline: Python help, accessing wikipedia pages





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC