任务2:在任务1的基础上,当学生回答错误时,最多给三次重做的机会,三次仍未做对,则显示” Wrong! You have tried three times! Test over!"。程序结束。
时间: 2024-03-24 10:39:29 浏览: 9
好的,理解您的要求了。以下是实现该功能的示例代码:
```
import random
# 定义题目和答案
questions = {
"What is the capital city of China?": "Beijing",
"What is the largest country in the world by land area?": "Russia",
"What is the currency of Japan?": "Yen",
"What is the highest mountain in the world?": "Mount Everest"
}
# 随机选择一道题目
question = random.choice(list(questions.keys()))
# 初始化重做次数
retry = 0
# 开始答题
while True:
answer = input(question + " ")
if answer == questions[question]:
print("Correct!")
break
else:
retry += 1
if retry >= 3:
print("Wrong! You have tried three times! Test over!")
break
else:
print("Wrong! Please try again.")
```
以上代码会随机选择一道题目,并在用户回答错误时最多给三次重做的机会。如果用户在三次之内未能做对,则程序结束,输出"Wrong! You have tried three times! Test over!"的提示信息。