【发布时间】:2015-05-25 16:49:50
【问题描述】:
下面是我的代码。它目前是关于基本学校测验的计划的一部分。我的问题是,当我尝试运行程序时,没有出现任何错误,但它只为用户提供了一个问题。有人可以向我解释为什么会这样。任何帮助深表感谢。
import random, math, sys, re, sqlite3, os
import operator as op
def inputName():
username=str(input("What is your name?"))
if not re.match("^[A-Za-z ]*$", username) or username=="":
username=input(str("Please enter a valid name (it must not contain numbers or symbols)."))
print("Hi {}! Wellcome to the Arithmetic quiz...".format(username))
username = None
inputName()
else:
return username
def test():
num1 = random.randint(1, 10)
num2 = random.randint(1, num1)
ops = {
'+': op.add,
'-': op.sub,
'*': op.mul,
}
keys = list(ops.keys())
rand_key = random.choice(keys)
operation = ops[rand_key]
correct_result = operation(num1, num2)
print ("What is {} {} {}?".format(num1, rand_key, num2))
while True:
try:
user_answer = int(input("Your answer: "))
except ValueError:
print("Only enter numbers!")
continue
else:
break
if user_answer != correct_result:
print ("Incorrect. The right answer is {}".format(correct_result))
return False
else:
print("Correct!")
return True
correct_answers = 0
num_questions = 10
for i in range(num_questions):
if test():
correct_answers +=1
print("{}: You got {}/{} correct.".format(username, correct_answers, num_questions))
return correct_answers
def makeDB():
if os.path.exists("the_quiz_scores.db.db") == False: #checks if file doesn't exists
makeFile = open("the_quiz_scores.db.db", "w") #if not, then it will create one
makeFile.close()
else:
return True
def mainProgram():
name = inputName()
maths = test()
mainProgram()
【问题讨论】:
-
请修正你的缩进
-
我在移动代码时不断遇到选项卡错误。 @IanAuld
-
您需要修复您的缩进,因为您的问题很可能是由不良缩进引起的,所以我无法为您编辑您的问题。单击标签下的
edit链接以编辑您的问题。
标签: python mysql python-3.x iteration