开发者

validating correct answer with loops in python

开发者 https://www.devze.com 2022-12-14 09:39 出处:网络
Sorry for the non descriptive question I had no idea how to word it. I\'m trying to write a program (GUI) where I ask the users questions and then in return they answer and see if they are correct how

Sorry for the non descriptive question I had no idea how to word it.

I'm trying to write a program (GUI) where I ask the users questions and then in return they answer and see if they are correct however when I enter the correct answer it's still showing as being incorrect.

My code looks something like this.

prompt for question 1

txtQuestion = Text(Point(5,8), "Question 1")
txtQuestion.setTextColor("red")
txtQuestion.setSize(16)
txtQuestion.setStyle("bold")
txtQuestion.draw(win)

txtAnswer = Text(Point(1.5,4), "Answer 1: ")
txtAnswer.setTextColor(color_rgb(255,127,80))
txtAnswer.setSize(14)
txtAnswer.setStyle("bold")
txtAnswer.draw(win)

txtAnswer2 = Text(Point(1.5,3), "Answer 2: ")
txtAnswer2.setTextColor(color_rgb(255,127,80))
txtAnswer2.setSize(14)
txtAnswer2.setStyle("bold")
txtAnswer2.draw(win)

txtAnswer3 = Text(Point(1.5,2), "Answer 3: ")
txtAnswer3.setTextColor(color_rgb(255,127,80))
txtAnswer3.setSize(14)
txtAnswer3.setStyle("bold")
txtAnswer3.draw(win)

txtAnswer4 = Text(Point(1.5,1), "Answer 4: ")
txtAnswer4.setTextColor(color_rgb(255,127,80))
txtAnswer4.setSize(14)
txtAnswer4.setStyle("bold")
txtAnswer4.draw(win)

txtEnterAn = Text(Point(8,3), "Enter your answer below: ")
txtEnterAn.setTextColor("black")
txtEnterAn.draw(win)

entAnswer = Entry(Point(8,2), 3)
entAnswer.draw(win)

Answer1 = entAnswer.getText()


win.getMouse()

#loop for answer
if Answer1 == "A":
     txtCorrect = Text(Point(5,9), "Correct!")
     txtCorrect.setTextColor("black")
     txtCorrect.draw(win)
else:
    txtCorrect = Text(Point(5,9), "Inorrect!")
    txtCorrect.setTextColor("black")
    txtCorrect.draw(win)

Now I'm not sure why every time I enter "A" it still shows as incorrect I know in another program I had to float the entAnswer variable but I figured this time I wo开发者_开发问答uldn't have to since it's a string.

I must be overlooking the situation but I can't lay my finger down on it, any help would be appreciated, thanks!

p.s. I didn't put it in with the code but I do have the variables up top initialized such as Answer1 = " " and so forth


The problem here seems to be that you're misunderstanding how GUIs work. It's not like the sequential print/read code that most programming instruction starts with. The GUI widgets only create themselves, draw to the screen and wait for events.

This line:

Answer1 = entAnswer.getText()

will end up setting Answer1 to an empty string, because at that point, the user hasn't entered anything in the text box. Instead, you have to create a callback function that will be called by the GUI when the user hits a button to score the answer. Then in that function you will be able to read the user's answer and mark it correct or incorrect.

I recommend going through your GUI library's tutorial again to get a feel for the event-driven style of GUI programming.


I'd recommend that you abstract that user interface detail away from the problem of showing questions, obtaining answers, and determining correctness. You can sort all of that out with nothing more than a command line, text based user interface. Once you have that, then you can proceed with the user interface design with confidence, knowing that the logic behind the questionnaire is sound.

This idea goes by several names: layering, MVC, etc. I'd recommend it for this problem, because it'll help you learn the idea for those more difficult problems to come where it'll be indispensable.


i don't see a reason the logic would fail, but are you sure you are pressing "A" and not "a" .


I can't say anything about this particular problem, but I would do a

print "'" + answer + "'"
print answer.__class__

I have encountered wrapper classes (in OTHER situations) which behave like strings but are not actually strings. Furthermore spaces and newlines can be added everywhere :)

0

精彩评论

暂无评论...
验证码 换一张
取 消