即使我正确回答PYTHON

时间:2015-10-21 13:22:06

标签: python

在python编程方面,我非常喜欢。但是我需要尽快制作一个测验制作程序,最后给我%分数。这是我到目前为止所做的:

#coding: utf-8

from random import shuffle

qas = [
('Kvarts', 'SiO2'),
('Gull', 'Au'),
('Sølv', 'Ag'),
('Kobber', 'Cu'),
('Platina', 'Pt'),
('Grafitt', 'C'),
('Diamant', 'C'),
]

shuffle(qas)
numRight = 0

for question, rightAnswer in qas:
answer = raw_input(question + ' ')
if answer.lower( ) == rightAnswer:
print 'Correct'
numRight +=1
else:
print 'Wrong, the correct answer is ' + rightAnswer
print 'You got %d and %d wrong.' % (numRight, len(qas) - numRight)

我让程序稍微有点工作,但即使我回答正确,它仍然给我错误的答案:

Diamant C
Wrong, the correct answer is C
Kvarts SiO2
Wrong, the correct answer is SiO2

我不知道如何解决这个问题。

3 个答案:

答案 0 :(得分:0)

列表中的答案不是小写的。如果用户输入C,您的条件将评估'C'.lower() == 'C'(仅举例)。尝试

if answer.lower() == rightAnswer.lower():

答案 1 :(得分:0)

使用

检查答案是否正确
User ID=1001, Name=17
User ID=1002, Name=Kim
User ID=1003, Name=Jane Koffman
User ID=1004, Name=Katherine H. Lang
User ID=1005, Name=Smith
User ID=1006, Name=Juanita J. Palmer
User ID=1007, Name=Otherone with no age
User ID=1008, Name=Ethel W. Williams
User ID=1009, Name=Someone with no age
User ID=1010, Name=Candis C. Whitehead
User ID=1011, Name=Kim
User ID=1012, Name=Margaret F. Delmonte
User ID=1013, Name=Susan M. McKeel
User ID=1014, Name=Kim
User ID=1015, Name=Shirley A. Dehaven
User ID=1016, Name=Smith
User ID=1017, Name=Chad G. Turner
User ID=1018, Name=Suzanne J. Champine
User ID=1019, Name=Harry King
Goodbye!

这会将if answer.lower( ) == rightAnswer: 转换为小写(例如answer转换为C

字典中的答案区分大小写:

c

您有3个选项:

1将字典中的答案转换为大写:

"C" == "c" => False

或2 :(相反)将所有内容更改为小写

或3:不要拨打qas = [ ('Kvarts', 'SIO2'), ('Gull', 'AU'), ('Sølv', 'AG'), ('Kobber', 'CU'), ('Platina', 'PT'), ('Grafitt', 'C'), ('Diamant', 'C'), ]

答案 2 :(得分:0)

您不需要致电.lower,因为这会返回带有小写字母的字符串,而正确答案可能包含大写字母。

您可以改为answer.lower()。这将删除输入字符串开头和结尾的空格。

另一种选择是简单地执行answer.strip(),但如果answer == rightAnswer被空格包围,则会产生'Wrong answer'