我的if语句不能在Python中正确循环使用
问题描述:
我只完成了1个学期的python,但是这看起来应该是工作而且我完全没有想法!基本上,我需要创建一个10问题测验,要求按随机顺序的问题,我面对的问题是,仅环承认第一次的问题,而不是通过对回答所有10我的if语句不能在Python中正确循环使用
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
答
使用正确的指数移动检查:
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
+0
我主要工作,但现在它重复同样的问题,我只需要问他们一次。 –
+0
删除'random.shuffle(问题)'(或注释它),以获得正确的序列。 – SatanDmytro
那么,是不是'[0]'看起来可疑?如果你想要其他的值,不要总是使用第一个索引 –
另外,而不是'questions2',你为什么不使用'print(“问题”+问题)'? –
它为我循环。当然,它并没有承认我的正确答案。 – toonarmycaptain