python中添加条件_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.\n", 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, “\nTest time is”, testTime, “seconds”)

————————————————

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值