Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I';我在试着给自己做一个关于布尔人的测试。不';你行为不正常吗?_Python - Fatal编程技术网

Python I';我在试着给自己做一个关于布尔人的测试。不';你行为不正常吗?

Python I';我在试着给自己做一个关于布尔人的测试。不';你行为不正常吗?,python,Python,我正在阅读Zed Shaw的《艰苦地学习Python》,并且我已经开始练习27,基本上是要记住布尔逻辑“表”。因此,我决定用python对此进行快速测试,但无论我做什么,它都无法正常运行。如果我把每一个正确的答案都写进去,它会给出大约40%的分数,而这个分数应该是100。脚本如下。正确答案是 这是因为列表索引从0开始,而不是1。因此,您的输入相对于答案移动了一位 如果块应该是: if q1 == Answers[0]: cout = cout + 3.84615385 else:

我正在阅读Zed Shaw的《艰苦地学习Python》,并且我已经开始练习27,基本上是要记住布尔逻辑“表”。因此,我决定用python对此进行快速测试,但无论我做什么,它都无法正常运行。如果我把每一个正确的答案都写进去,它会给出大约40%的分数,而这个分数应该是100。脚本如下。正确答案是


这是因为列表索引从0开始,而不是1。因此,您的输入相对于
答案
移动了一位

如果块应该是:

if q1 == Answers[0]:
    cout = cout + 3.84615385
else:
    cout = cout + 0.0
最后一点:

if q26 == Answers[25]:
    cout = cout + 3.84615385
else:
    cout = cout + 0.0

这是因为列表索引从0开始,而不是1。因此,您的输入相对于
答案
移动了一位

如果块应该是:

if q1 == Answers[0]:
    cout = cout + 3.84615385
else:
    cout = cout + 0.0
最后一点:

if q26 == Answers[25]:
    cout = cout + 3.84615385
else:
    cout = cout + 0.0

编辑:有人打败了我! 答案是一个数组。数组中的第一个元素以0而不是1开头。因此,您应该从if子句中的答案[0]开始。 我可以这样写代码:

import time

Answers = ["True", "False", "True", "True", "True", "False", "False","True","False", "False", "False", "False", "False", "True", "True","False", "True", "True", "True", "False", "True", "False", "False","True", "False", "True"]

q1 = raw_input("not False = "); q2 = raw_input("not True = "); 
q3 = raw_input("True or False = "); q4 = raw_input("True or True = "); 
q5 = raw_input("False or True = "); 
q6 = raw_input("False or False = "); 
q7 = raw_input("True and False = "); q8 = raw_input("True and True = "); 
q9 = raw_input("False and True = "); 
q10 = raw_input("False and False = "); 
q11 = raw_input("not (True or False) = "); 
q12 = raw_input("not (True or True) = "); 
q13 = raw_input("not (False or True) = "); q14 = raw_input("not (False or False) = "); 
q15 = raw_input("not (True and False) = "); 
q16 = raw_input("not (True and True) = "); q17 = raw_input("not (False and True) = "); 
q18 = raw_input("not (False and False) = "); 
q19 = raw_input("1 != 0 = "); q20 = raw_input("1 != 1 = "); 
q21 = raw_input("0 != 1 = "); q22 = raw_input("0 != 0 = "); 
q23 = raw_input("1 == 0 = "); q24 = raw_input("1 == 1 = "); 
q25 = raw_input("0 == 1 = "); 
q26 = raw_input("0 == 0 = ");
Questions =[q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24,q25,q26]

count = sum([3.8461 for i in range(26) if Questions[i] == Answers[i]])

print "Calculating results..."
time.sleep(2)
print("""
=============================
    Your Score Was:
          %s percent
=============================""" %str(cout))

编辑:有人打败了我! 答案是一个数组。数组中的第一个元素以0而不是1开头。因此,您应该从if子句中的答案[0]开始。 我可以这样写代码:

import time

Answers = ["True", "False", "True", "True", "True", "False", "False","True","False", "False", "False", "False", "False", "True", "True","False", "True", "True", "True", "False", "True", "False", "False","True", "False", "True"]

q1 = raw_input("not False = "); q2 = raw_input("not True = "); 
q3 = raw_input("True or False = "); q4 = raw_input("True or True = "); 
q5 = raw_input("False or True = "); 
q6 = raw_input("False or False = "); 
q7 = raw_input("True and False = "); q8 = raw_input("True and True = "); 
q9 = raw_input("False and True = "); 
q10 = raw_input("False and False = "); 
q11 = raw_input("not (True or False) = "); 
q12 = raw_input("not (True or True) = "); 
q13 = raw_input("not (False or True) = "); q14 = raw_input("not (False or False) = "); 
q15 = raw_input("not (True and False) = "); 
q16 = raw_input("not (True and True) = "); q17 = raw_input("not (False and True) = "); 
q18 = raw_input("not (False and False) = "); 
q19 = raw_input("1 != 0 = "); q20 = raw_input("1 != 1 = "); 
q21 = raw_input("0 != 1 = "); q22 = raw_input("0 != 0 = "); 
q23 = raw_input("1 == 0 = "); q24 = raw_input("1 == 1 = "); 
q25 = raw_input("0 == 1 = "); 
q26 = raw_input("0 == 0 = ");
Questions =[q1,q2,q3,q4,q5,q6,q7,q8,q9,q10,q11,q12,q13,q14,q15,q16,q17,q18,q19,q20,q21,q22,q23,q24,q25,q26]

count = sum([3.8461 for i in range(26) if Questions[i] == Answers[i]])

print "Calculating results..."
time.sleep(2)
print("""
=============================
    Your Score Was:
          %s percent
=============================""" %str(cout))

您可以创建一个问答对列表,而不是处理索引和硬编码值。然后使用for循环遍历问题。这将允许您添加或删除问题,而无需更改程序中的任何其他内容

import time

# This list contains tuples where the first element is the question and the second the answer.
questions = [
    ('not False = ', 'True'),
    ('not True = ', 'False'),
    ('True or False = ', 'True'),
    ('True or True = ', 'True'),
    ('False or True = ', 'True'),
    ('False or False = ', 'False'),
    ('True and False = ', 'False'),
    ('True and True = ', 'True'),
    ('False and True = ', 'False'),
    ('False and False = ', 'False'),
    ('not (True or False) = ', 'False'),
    ('not (True or True) = ', 'False'),
    ('not (False or True) = ', 'False'),
    ('not (False or False) = ', 'True'),
    ('not (True and False) = ', 'True'),
    ('not (True and True) = ', 'False'),
    ('not (False and True) = ', 'True'),
    ('not (False and False) = ', 'True'),
    ('1 != 0 = ', 'True'),
    ('1 != 1 = ', 'False'),
    ('0 != 1 = ', 'True'),
    ('0 != 0 = ', 'False'),
    ('1 == 0 = ', 'False'),
    ('1 == 1 = ', 'True'),
    ('0 == 1 = ', 'False'),
    ('0 == 0 = ', 'True')
]

score = 0
number_of_questions = len(questions)

for question, answer in questions:
    if raw_input(question) == answer:
        score += 100.0 / number_of_questions

print "Calculating results..."
time.sleep(2)
print """
=============================
    Your Score Was:
          %s percent
=============================
""" % str(score)

您可以创建一个问答对列表,而不是处理索引和硬编码值。然后使用for循环遍历问题。这将允许您添加或删除问题,而无需更改程序中的任何其他内容

import time

# This list contains tuples where the first element is the question and the second the answer.
questions = [
    ('not False = ', 'True'),
    ('not True = ', 'False'),
    ('True or False = ', 'True'),
    ('True or True = ', 'True'),
    ('False or True = ', 'True'),
    ('False or False = ', 'False'),
    ('True and False = ', 'False'),
    ('True and True = ', 'True'),
    ('False and True = ', 'False'),
    ('False and False = ', 'False'),
    ('not (True or False) = ', 'False'),
    ('not (True or True) = ', 'False'),
    ('not (False or True) = ', 'False'),
    ('not (False or False) = ', 'True'),
    ('not (True and False) = ', 'True'),
    ('not (True and True) = ', 'False'),
    ('not (False and True) = ', 'True'),
    ('not (False and False) = ', 'True'),
    ('1 != 0 = ', 'True'),
    ('1 != 1 = ', 'False'),
    ('0 != 1 = ', 'True'),
    ('0 != 0 = ', 'False'),
    ('1 == 0 = ', 'False'),
    ('1 == 1 = ', 'True'),
    ('0 == 1 = ', 'False'),
    ('0 == 0 = ', 'True')
]

score = 0
number_of_questions = len(questions)

for question, answer in questions:
    if raw_input(question) == answer:
        score += 100.0 / number_of_questions

print "Calculating results..."
time.sleep(2)
print """
=============================
    Your Score Was:
          %s percent
=============================
""" % str(score)

请不要把几句话放在同一行。你的代码是无法阅读的。我会记住这一点。我通常这么做是因为这样比较容易@フェルナンド 在本例中,
else
-块不起任何作用,因此可以完全删除它,从而为您节省50行代码。另外,如果你把问题放在一个列表中,你可以使用for循环来处理所有问题,这样你就可以节省额外的50行(大约)。我的主要问题是我对诸如循环或逻辑之类的函数不太了解,这导致我在不知不觉中添加了额外和不必要的代码。谢谢你的建议@フェルナンド 我明白,只是想我会建议。我给了你一个答案,告诉你我的意思,但如果这太难了,那么我道歉。请不要把几句话放在同一行。你的代码是无法阅读的。我会记住这一点。我通常这么做是因为这样比较容易@フェルナンド 在本例中,
else
-块不起任何作用,因此可以完全删除它,从而为您节省50行代码。另外,如果你把问题放在一个列表中,你可以使用for循环来处理所有问题,这样你就可以节省额外的50行(大约)。我的主要问题是我对诸如循环或逻辑之类的函数不太了解,这导致我在不知不觉中添加了额外和不必要的代码。谢谢你的建议@フェルナンド 我明白,只是想我会建议。我做了一个回答,告诉你我的意思,但如果这太难,那么我道歉。啊,我明白了。现在我觉得自己很愚蠢。下次我会记得的。谢谢啊,我明白了。现在我觉得自己很愚蠢。下次我会记得的。谢谢我不建议使用分号来避免代码换行。它没有可读性和非传统性。我还建议您使用
小写字母
命名变量,并避免使用以大写字母开头的名称,因为它们用于命名类。我不建议使用分号来避免代码换行。它没有可读性和非传统性。我也会用
lower_case
命名变量,并避免使用以大写字母开头的名称,因为它们用于命名类。