我的if语句在Python

时间:2017-10-14 00:31:35

标签: python python-3.x

我只完成了一个学期的python,但这看起来应该是有效的,而且我的所有想法都没有!基本上我需要创建一个10个问题的测验,以随机顺序询问问题,我面临的问题是循环只承认第一个问题并且没有通过所有10个问题

import random
questions = ["2x2", "4x4", "3x3", "5x5", "0x100", "6x2", "10x10", "5x7", 
"9x8" ,"9x9"]
questions2 = ["Question 1", "Question 2", "Question 3", "Question 4", 
"Question 5", "Question 6", "Question 7", "Question 8", "Question 9" 
,"Question 10"]
num1 = [2, 4, 3, 5, 0, 6, 10, 5, 9, 9]
num2 = [2, 4, 3, 5, 100, 2, 10, 7, 8, 9]
ans = [4, 16, 9, 25, 0, 12, 100, 35, 75, 81]
random.randint(0,9)
random.shuffle(questions)
question2 = 0
question = 0
while question < 10:
    print ((questions2[question]), (questions[question]))
    user_typed_ans= int(input())
    if user_typed_ans==num1[0]* num2[0]:
        print ("Correct")
    else:
        print ("Incorrect:")
        print(ans[0])
    question += 1

1 个答案:

答案 0 :(得分:0)

使用正确的索引进行回答检查:

import random

questions = ["2x2", "4x4", "3x3", "5x5", "0x100", "6x2", "10x10", "5x7", "9x8" ,"9x9"]
questions2 = ["Question 1", "Question 2", "Question 3", "Question 4", 
"Question 5", "Question 6", "Question 7", "Question 8", "Question 9" 
,"Question 10"]
num1 = [2, 4, 3, 5, 0, 6, 10, 5, 9, 9]
num2 = [2, 4, 3, 5, 100, 2, 10, 7, 8, 9]
ans = [4, 16, 9, 25, 0, 12, 100, 35, 75, 81]

# Comment this for correct answers
# random.shuffle(questions)

question = 0
while question < 10:
    print ((questions2[question]), (questions[question]))
    user_typed_ans = int(input())
    if user_typed_ans == num1[question] * num2[question]:
        print ("Correct")
    else:
        print ("Incorrect:")
        print (ans[question])
    question += 1