20200617Python学习记录 之往while添加一个条件

import random
import time

correctCount = 0 # Count the number of correct answers
count = 0 # Count the number of questions
NUMBER_OF_QUESTIONS = 5 # Constant
continueLoop = ‘Y’
startTime = time.time() # Get start time

**

while continueLoop == ‘Y’ and count < NUMBER_OF_QUESTIONS:

今天学习中在这里做了一个自己最满意也是最开心的设计。增加了一个and语句,将书里要求的每次输入一个Y来决定是否循环。和原来的5次之后停止循环结合起来。开始的时候我还想着分开两个while来写,但是怎么写的不对。最后灵光一闪。哈哈,在这里加了一个and把5次记数加进去了。
不过还有个小插曲。我加5次记数的时候写成了。NUMBER_OF_QUESTIONS
<5。没有报错。但是只要运行程序就直接提示运行时间为0,次数为了0。后来自己往下看了一眼原来的内容才明白写错了。这个小插曲挺有意思。哈哈,以后研究。谢谢大家欣赏
**
# while count < NUMBER_OF_QUESTIONS:
# Generate two random single-digit integers
number1 = random.randint(0, 9)
number2 = random.randint(0, 9)

# If number1 < number2, swap number1 with number2
if number1 < number2:
number1, number2 = number2, number1

# Prompt the student to answer "What is number1 - number2?"
answer = eval(input("What is " + str(number1) + " - " +
str(number2) + "? "))

# Grade the answer and display the result
if number1 - number2 == answer:
print("You are correct!")
correctCount += 1
else:
print("Your answer is wrong. ", number1, "-",
number2, "is", number1 - number2)
# Increase the count
continueLoop = input("Enter Y to continue and N to quit: ")
count += 1

endTime = time.time() # Get end time
testTime = int(endTime - startTime) # Get test time

print(“Correct count is”, correctCount, “out of”,
NUMBER_OF_QUESTIONS, “ Test time is”, testTime, “seconds”)
————————————————

原文地址:https://www.cnblogs.com/yogaMan/p/13150416.html