二十以内加减法作业题生成
时间: 2024-04-24 12:27:06 浏览: 197
根据引用\[1\]和引用\[2\]的代码,可以生成二十以内的加减法作业题。以下是生成的代码示例:
```python
import random
def generate_add_sub_questions():
questions = \[\]
for i in range(50):
num1 = random.randint(0, 20)
num2 = random.randint(0, 20)
if random.choice(\[True, False\]):
question = "{}+{}=".format(num1, num2)
answer = num1 + num2
else:
if num1 >= num2:
question = "{}-{}=".format(num1, num2)
answer = num1 - num2
else:
continue
questions.append((question, answer))
return questions
questions = generate_add_sub_questions()
print("学号:*** 姓名:***")
for index, question in enumerate(questions):
if (index + 1) % 5 == 0:
print(question\[0\])
else:
print(question\[0\], end='\t')
```
这段代码会生成50道二十以内的加减法作业题,并按照每行5道题的格式输出。每道题的格式为`num1+num2=`或`num1-num2=`,其中`num1`和`num2`是随机生成的数字。
#### 引用[.reference_title]
- *1* [Python妙用|给小外甥生成10以内加减运算数学作业](https://blog.csdn.net/a55656aq/article/details/122984822)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [python作业——随机生成不重复的20以内加法算式](https://blog.csdn.net/Pan_77777/article/details/125164497)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文