python算数小游戏
时间: 2023-11-08 13:04:56 浏览: 98
python小游戏
好的,我可以为您提供一个简单的Python算数小游戏。以下是代码:
```
import random
def generate_question():
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
operator = random.choice(['+', '-', '*'])
question = f"What is {num1} {operator} {num2}?"
answer = eval(f"{num1}{operator}{num2}")
return question, answer
def main():
score = 0
for i in range(5):
question, answer = generate_question()
user_answer = input(question + " ")
if int(user_answer) == answer:
print("Correct!")
score += 1
else:
print(f"Incorrect. The correct answer is {answer}.")
print(f"Your final score is {score}/5.")
if __name__ == '__main__':
main()
```
这个小游戏会随机生成5个算术问题,每个问题包含两个1到10之间的数字和一个加减乘中的一个运算符。玩家需要输入答案,程序会判断答案是否正确,并给出相应的提示。最后,程序会输出玩家的得分。
阅读全文