Help with code please!

This is the place for queries that don't fit in any of the other categories.

Help with code please!

Postby aparsaa » Wed Jun 12, 2013 1:31 am

Im new to python and was hoping I could get some help on a VERY VERY basic quiz game i am creating for a tutorial to teach me python. While helping me please keep in mind I have ABSOLUTELY NO previous programming experience and am a week old to Python. I have 2 problems with my code.

For one, I don't know how to make the multiple choice question I made case insensitive, i can only make the entries 3 and C correct and everything else incorrect. the questionTwo variable that is.

Secondly Id like a less archaic scoring system if you guys could help me come up with something a bit better, I feel the one I am creating now has many unnecessary lines.

If there is also any way i can improve on this code please let me know! Remember, I am BRAND NEW! Dont get too technical with me without too much information!

here is my code
Code: Select all
X = 0
questionOne = float(input("What is 1+1?"))
if questionOne == 2:
    print"Correct!"
else:
    print"Incorrect."
if questionOne == 2:
    X = X + 1
    print"Your score is: ",X
else:
    X = X
    print"Your score is: ",X
A = 1
B = 2
C = 3
questionTwo= input("Which number is highest?\nA:1\nB:2\nC:3\n")
if questionTwo == 3:
    print"Correct!"
else:
    print"Incorrect."
if questionTwo == 3 or "c":
    X = X + 1
    print"Your score is: ",X
else:
    X = X
    print"Your score is: ",X
aparsaa
 
Posts: 1
Joined: Sat Jun 08, 2013 5:05 pm

Re: Help with code please!

Postby micseydel » Wed Jun 12, 2013 2:31 am

The line
Code: Select all
if questionTwo == 3 or "c":

is an example of a common pitfall. To Python, it is equivalent to
Code: Select all
if (questionTwo == 3) or ("c"):

What happens here is that if questionTwo is three, then it's true, and if not then it takes whatever the boolean value of "c" is, which is always true. Non-empty strings are treated as true. What you want is
Code: Select all
if questionTwo == 3 or questionTwo == "c":

or, better,
Code: Select all
if questionTwo in (3, "c"):

This last version is the typical way of checking if user input is in a set of possible valid answers. Also, you should put the 3 in quotes because it's a string and not a number, you never turned it into a number, and never need to since you can compare it as a string. As such, you can use the .lower() string method to achieve case insensitivity.
Code: Select all
if questionTwo.lower() in ("3", "c"):

As for having too many lines of code, I would do it something like
Code: Select all
questions_and_answers = [
    ("question?", ("acceptable", "answers")),
    ("question2?", ("more", acceptable", "answers")),
]

for question, answers in questions_and_answers:
    #some standard way of asking the question and checking the answer


If you've never seen something like
Code: Select all
for question, answers in questions_and_answers:

with the comma toward the beginning, it's like this
Code: Select all
pairs = [
    (1, 2),
    (3, 4),
    (5, 6)
]

for high, low in pairs:
    print high, low

I tried to be very thorough, but left the loop body for you to fill in or ask questions about.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 940
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Help with code please!

Postby Mekire » Wed Jun 12, 2013 2:32 am

First off. Are you using python 2 or 3?

Next as for making the code expandable and flexible I recommend putting question answer pairs in a list. Then all you need to do is add questions to that list and the code will just work.

Here is an example:
Code: Select all
Q_AND_A = [("What is 1+1?",'2'),
           ("Which number is highest?\nA: 1\nB: 2\nC: 3\n",'c')]

score = 0
for question,answer in Q_AND_A:
    user = input(question).lower()
    if user == answer:
        score += 1
        print("Correct!")
        print("Your current score is: {}\n".format(score))
    else:
        print("Incorrect.\n")
print("Your final score is: {}".format(score))

Generally we would also make this a function to avoid using global variables but that is more complex.

-Mek
User avatar
Mekire
 
Posts: 828
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan


Return to General Coding Help

Who is online

Users browsing this forum: Marbelous and 2 guests