[Tutor] My First Program
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sat Dec 10 03:31:52 CET 2005
> def quiz_question(some_question, some_right_answer):
> question = raw_input(some_question)
> if question == some_right_answer:
> print "Yes!\n"
> else:
> print "Wrong\n"
>
> quiz_question("Name the capital of NC? ", "Raleigh")
> quiz_question("Name the capital of SC? ", "Columbia")
> quiz_question("Name the capital of NY? ", "Albany")
> quiz_question("Name the capital of OH? ", "Columbus")
> quiz_question("Name the capital of TX? ", "Austin")
Hi Trent,
Yeah, much better. *grin*
> Speaking of stuck, I'm not sure how to create the counting variable that
> Greg suggested. Can someone lead me but don't tell me the answer?
> Thanks!
One possible way to do this is to modify quiz_question() so that it
doesn't just print out "Yes" or "Wrong" as a side effect, but is actively
involved in maintaining the counter.
One way to do this is to modify quiz_question() to take in the number of
chances the player has before asking the question, and have it return the
number of chances after asking the question. So we might say something
like:
quiz_question("Name the capital of NC? ", "Raleigh", 5)
and expect to either see 5 or 4 as the return value.
Alternatively, you can modify quiz_question so that it actively changes a
global variable. I don't like globals, but for what you're doing, it also
makes sense to use them.
If you have questions, please feel free to ask!
More information about the Tutor
mailing list