# 1. 輸出100以內加法題試卷

import random  # 因爲要隨機產生加數 所以肯定要使用到random

# 注意: 一般來說我們所說的100以內的加法題是指的結果不超過100
# 這裏筆者也硬性規定 加數不能=100 最多爲99
# 因爲每次進入循環express1和express2都會發生變化 所以要用一個空的字符串每次去拼接不同的表達式
# 這樣最後就會把20個表達式都拼接在一起
str1 = ""
str2 = ""

for i in range(20):  # 循環20次 每次生成一道題
num1 = random.randint(0, 99)  # 隨機產生加數1
num2 = random.randint(0, 99 - num1)  # 隨機產生加數2
result = num1 + num2  # 結果
# 開始拼接字符串 因爲有些加數是有些是2位 有些是1位 所以我們要進行對齊處理 最多2位 不足的以空白填充
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
# 拼接不帶答案的表達式: 74 + 20 =
express1 = num1 + " + " + num2 + " = "
# 拼接帶答案的表達式: 74 + 20 = 94
express2 = num1 + " + " + num2 + " = " + str(result)
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print("\033[0;34m20道100以內加法題:")
print(str1)
print("20道以內加法題(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 2. 輸出100以內減法題試卷

import random

str1 = ""
str2 = ""
for i in range(20):  # 循環20次 每次生成一道題
num1 = random.randint(0, 99)  # 隨機產生數1
num2 = random.randint(0, 99 - num1)  # 隨機產生數2
# 因爲結果不能出現負數 所以要進行判斷
if num1 < num2:
num1, num2 = num2, num1
result = num1 - num2  # 被減數num1始終爲最大的那個數
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " - " + num2 + " = "
express2 = num1 + " - " + num2 + " = " + str(result).ljust(2, " ")  # 這裏的結果也進行了左對齊
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print("\033[0;34m20道100以內減法題:")
print(str1)
print("20道以內減法題(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 3. 輸出100以內加減法題試卷

import random

str1 = ""
str2 = ""
for i in range(20):  # 循環20次 每次生成一道題
num1 = random.randint(0, 99)  # 隨機產生數1
num2 = random.randint(0, 99 - num1)  # 隨機產生數2
symbol = random.choice("+-")
# 判斷符號是加號 還是減號
if symbol == "+":
result = num1 + num2  # 是+號 做加法
else:
if num1 < num2:
num1, num2 = num2, num1
result = num1 - num2  # 否則做減法
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " " + symbol + " " + num2 + " = "
express2 = num1 + " " + symbol + " " + num2 + " = " + str(result).ljust(2, " ")
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print("\033[0;34m20道100以內加減法混合題試卷:")
print(str1)
print("20道100以內加減法混合題試卷(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 4. 輸出乘數100以內的乘法題試卷

import random

str1 = ""
str2 = ""
num = int(input("請輸入需要生成的題目數: 如(30): ").strip())
for i in range(num):  # 循環num次 每次生成一道題
num1 = random.randint(0, 99)  # 隨機產生乘數1
num2 = random.randint(0, 99 - num1)  # 隨機產生乘數2
result = num1 * num2  # 結果
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " * " + num2 + " = "
express2 = num1 + " * " + num2 + " = " + str(result).ljust(4, " ")  # 這裏的結果也進行了左對齊
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print(f"\033[0;34m{num}道100以內乘法題試卷:")
print(str1)
print(f"{num}道100以內乘法題試卷(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 5. 隨機輸出100以內除法題試卷

import random

str1 = ""
str2 = ""
num = int(input("請輸入需要生成的題目數: 如(30): ").strip())
for i in range(num):  # 循環num次 每次生成一道題
num1 = random.randint(1, 99)  # 隨機產生被除數
num2 = random.randint(1, 99 - num1)  # 隨機產生除數2 注意除數不能爲0
# 因爲要求最後結果必須是整數 所以要進行判斷
if num1 < num2:
num1, num2 = num2, num1  # 被除數肯定要比除數大
while num1 % num2 != 0:
if num1 < 99:
num1 += 1
else:
if num2 > 1:
num2 -= 1
result = num1 // num2  # 結果
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " ÷ " + num2 + " = "
express2 = num1 + " ÷ " + num2 + " = " + str(result).ljust(2, " ")  # 這裏的結果也進行了左對齊
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print(f"\033[0;34m{num}道100以內除法題試卷:")
print(str1)
print(f"{num}道100以內除法題試卷(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 6. 隨機輸出100以內混合乘除法題

import random

str1 = ""
str2 = ""
num = int(input("請輸入需要生成的題目數: 如(30): ").strip())
for i in range(num):  # 循環num次 每次生成一道題
num1 = random.randint(1, 99)  # 隨機產生數1
num2 = random.randint(1, 99 - num1)  # 因爲有除數所以都從1開始
symbol = random.choice("*÷")
# 判斷符號是* 還是÷號
if symbol == "*":
result = num1 * num2  # 是*號 做乘法
else:
if num1 < num2:
num1, num2 = num2, num1
while num1 % num2 != 0:
if num1 < 99:
num1 += 1
else:
if num2 > 1:
num2 -= 1
result = num1 // num2  # 否則做除法
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " " + symbol + " " + num2 + " = "
express2 = num1 + " " + symbol + " " + num2 + " = " + str(result).ljust(2, " ")
if i % 2 != 0:
str1 += express1 + "\n"
str2 += express2 + "\n"
else:
str1 += express1 + "\t"
str2 += express2 + "\t"
with open("math.txt", "w", encoding="utf8") as file:
file.write(str1)
with open("key.txt", "w", encoding="utf8") as file:
file.write(str2)
print("\033[0;34m20道100以內乘除法混合題試卷:")
print(str1)
print("20道100以內乘除法混合題試卷(帶答案): ")
print(str2, end="")
print("\033[0m", end="")

# 7. 100以內加減法小測試

import random

print("----------100以內加減法小測試----------\n")
score = 0  # 用來記錄分數
msg = "當前"
for i in range(5):  #
num1 = random.randint(0, 99)  # 隨機產生數1
num2 = random.randint(0, 99 - num1)  # 隨機產生數2
symbol = random.choice("+-")
# 判斷符號是加號 還是減號
if symbol == "+":
result = num1 + num2  # 是+號 做加法
else:
if num1 < num2:
num1, num2 = num2, num1
result = num1 - num2  # 否則做減法
num1 = str(num1).ljust(2, " ")
num2 = str(num2).ljust(2, " ")
express1 = num1 + " " + symbol + " " + num2 + " = ?"
print(f"第{i + 1}題: {express1}")