Python3.xでPDFの計算プリントを作りたいと思って作ってみたのですが、あまりにもコードが長く汚く思えたのでよろしければアドバイスご教授ください!
from fpdf import FPDF import random pdf = FPDF('P', 'mm', 'Letter') pdf.set_auto_page_break(auto=True, margin = 10) pdf.add_page() pdf.set_font('helvetica', "", 16) def Addition(): global Add_FormerNumbers, Add_LatterNumbers, Add_SumNumbers Add_FormerNumbers = [] Add_LatterNumbers = [] Add_SumNumbers = [] for i in range(25): Add_FormerNumber = random.randint(2, 1000) Add_LatterNumber = random.randint(2, 100) Add_FormerNumbers.append(Add_FormerNumber) Add_LatterNumbers.append(Add_LatterNumber) Add_Sum = Add_FormerNumber + Add_LatterNumber Add_SumNumbers.append(Add_Sum) pdf.cell(50, 10, f"{Add_FormerNumber} + {Add_LatterNumber} =", ln=1) def Substraction(): pdf.set_xy(65, 10) global Sub_FormerNumbers, Sub_LatterNumbers, Sub_SumNumbers Sub_FormerNumbers = [] Sub_LatterNumbers = [] Sub_SumNumbers = [] for i in range(25): pdf.set_x(65) while True: Sub_FormerNumber = random.randint(1, 1000) Sub_LatterNumber = random.randint(1, 100) if Sub_FormerNumber > Sub_LatterNumber: Sub_Sum = Sub_FormerNumber - Sub_LatterNumber Sub_SumNumbers.append(Sub_Sum) Sub_FormerNumbers.append(Sub_FormerNumber) Sub_LatterNumbers.append(Sub_LatterNumber) pdf.cell(50, 10, f"{Sub_FormerNumber} - {Sub_LatterNumber} =", ln=1) break def Multiplication(): global Mul_FormerNumbers, Mul_LatterNumbers, Mul_SumNumbers Mul_FormerNumbers = [] Mul_LatterNumbers = [] Mul_SumNumbers = [] pdf.set_xy(110, 10) for i in range(25): pdf.set_x(110) Mul_FormerNumber = random.randint(1, 100) Mul_LatterNumber = random.randint(1, 10) Mul_SumNum = Mul_FormerNumber * Mul_LatterNumber Mul_FormerNumbers.append(Mul_FormerNumber) Mul_LatterNumbers.append(Mul_LatterNumber) Mul_SumNumbers.append(Mul_SumNum) pdf.cell(50, 10, f"{Mul_FormerNumber} × {Mul_LatterNumber} =", ln=1) def Division(): global Div_FormerNumbers, Div_LatterNumbers, Div_SumNumbers Div_FormerNumbers = [] Div_LatterNumbers = [] Div_SumNumbers = [] pdf.set_xy(160, 10) for i in range(25): pdf.set_x(160) while True: Div_FormerNumber = random.randint(1, 1000) Div_LatterNumber = random.randint(2, 100) Div_Sum = Div_FormerNumber % Div_LatterNumber if Div_Sum == 0: Div_SumNum = round(Div_FormerNumber / Div_LatterNumber) Div_FormerNumbers.append(Div_FormerNumber) Div_LatterNumbers.append(Div_LatterNumber) Div_SumNumbers.append(Div_SumNum) pdf.cell(50, 10, f"{Div_FormerNumber} ÷ {Div_LatterNumber} =", ln=1) break def AnswerSheet(): for n in range(25): pdf.cell(50, 10, f"{Add_FormerNumbers[n]} + {Add_LatterNumbers[n]} = {Add_SumNumbers[n]}", ln=1) pdf.set_xy(65, 10) for n in range(25): pdf.set_x(65) pdf.cell(50, 10, f"{Sub_FormerNumbers[n]} - {Sub_LatterNumbers[n]} = {Sub_SumNumbers[n]}", ln=1) pdf.set_xy(110, 10) for n in range(25): pdf.set_x(110) pdf.cell(50, 10, f"{Mul_FormerNumbers[n]} × {Mul_LatterNumbers[n]} = {Mul_SumNumbers[n]}", ln=1) pdf.set_xy(160, 10) for n in range(25): pdf.set_x(160) pdf.cell(50, 10, f"{Div_FormerNumbers[n]} ÷ {Div_LatterNumbers[n]} = {Div_SumNumbers[n]}", ln=1) def CalculateMaker(): Addition() Substraction() Multiplication() Division() AnswerSheet() for k in range(2): CalculateMaker() pdf.output("pdf_1.pdf")
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/05/26 09:25
退会済みユーザー
2021/05/26 10:54