|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
如代码所示,想实现输入Y后,程序再次执行五次测试并最终给出测试结果。但是如下代码会无限循环,并不会进入嵌套中的while循环中的各项if代码。
请各位大神看下原因,谢谢!#10以内减法小测试
# 1、随机生成1-9的整数,将两个整数相减,且结果不能为负数 2、提示用户输入正确答案 3、验证并告知用户回答结果,如果错误,则告诉用户正确答案
# 4、记录正确和错误答案次数 5、测试5次,结束后告知用户得分情况
import random
import time
a = 1
b = 9
amount_count = 5
coorect_answer_count = 0
answer_count = 1
start_time = time.time()
print ("让我们来进行一项10以内的减法小测试,\n共计",amount_count,"道题目!")
continue_loop = 'Y'
while continue_loop == 'Y':
while answer_count <= amount_count:
number1 = random.randint(a, b)
number2 = random.randint(a, b)
#数字转换,确保前面的数字大于后面数字,避免出现结果为负数
if number1 < number2 :
number1,number2 = number2,number1
#开始提问
answer = eval(input(str(number1)+"-"+str(number2)+"= ? "))
if answer == number1 - number2 :
print ("回答>>>正确<<<,答案为:",number1 - number2)
#正确回答次数+1
coorect_answer_count += 1
elif answer != number1 - number2 :
print("回答>>>错误<<<,",number1,"-",number2,"应该等于",number1-number2)
#回答次数+1
answer_count += 1
#跳出循环后,记录结束时间
end_time = time.time()
#计算测试时间
test_time = int(end_time - start_time)
#告诉用户测试结果
print (amount_count,"道题目已完成!")
print ("回答正确",coorect_answer_count,"次;错误",amount_count-coorect_answer_count,"次。\n总共用时",test_time,"秒!")
continue_loop = input ("小朋友,你是否愿意继续挑战?如果愿意,请输入“Y”,如果不愿意,直接退出即可!")
本帖最后由 qiuyouzhi 于 2020-3-14 21:45 编辑
最后一行缩进错误: #10以内减法小测试
# 1、随机生成1-9的整数,将两个整数相减,且结果不能为负数 2、提示用户输入正确答案 3、验证并告知用户回答结果,如果错误,则告诉用户正确答案
# 4、记录正确和错误答案次数 5、测试5次,结束后告知用户得分情况
#10以内减法小测试
# 1、随机生成1-9的整数,将两个整数相减,且结果不能为负数 2、提示用户输入正确答案 3、验证并告知用户回答结果,如果错误,则告诉用户正确答案
# 4、记录正确和错误答案次数 5、测试5次,结束后告知用户得分情况
import random
import time
a = 1
b = 9
amount_count = 5
coorect_answer_count = 0
answer_count = 1
start_time = time.time()
print ("让我们来进行一项10以内的减法小测试,\n共计",amount_count,"道题目!")
continue_loop = 'Y'
while continue_loop == 'Y':
while answer_count <= amount_count:
number1 = random.randint(a, b)
number2 = random.randint(a, b)
#数字转换,确保前面的数字大于后面数字,避免出现结果为负数
if number1 < number2 :
number1,number2 = number2,number1
#开始提问
answer = eval(input(str(number1)+"-"+str(number2)+"= ? "))
if answer == number1 - number2 :
print ("回答>>>正确<<<,答案为:",number1 - number2)
#正确回答次数+1
coorect_answer_count += 1
elif answer != number1 - number2 :
print("回答>>>错误<<<,",number1,"-",number2,"应该等于",number1-number2)
#回答次数+1
answer_count += 1
#跳出循环后,记录结束时间
end_time = time.time()
#计算测试时间
test_time = int(end_time - start_time)
#告诉用户测试结果
print (amount_count,"道题目已完成!")
print ("回答正确",coorect_answer_count,"次;错误",amount_count-coorect_answer_count,"次。\n总共用时",test_time,"秒!")
a = 1
b = 9
amount_count = 5
coorect_answer_count = 0
answer_count = 1
continue_loop = input ("小朋友,你是否愿意继续挑战?如果愿意,请输入“Y”,如果不愿意,直接退出即可!")
|
|