LoginSignup
2
0

More than 1 year has passed since last update.

三桁までの金額の最小硬貨枚数を調べるコード

Last updated at Posted at 2022-08-27

指定した金額を100,10,1円玉だけで支払う時に、それぞれ何枚ずつ必要になるか計算して表示するコード

import tkinter as tk

def dispLabel():
    a = int(EditBox1.get())
    A1=a//100
    A2=(a-A1*100)//10
    A3=(a-A1*100)-A2*10
    str_A1=str(A1)
    str_A2=str(A2)
    str_A3=str(A3)
    str_a=str(a)
    
    lbl2.configure(text=str_a+"は100円玉"+str_A1+"枚と")
    lbl3.configure(text="10円玉"+str_A2+"枚と")
    lbl4.configure(text="1円玉"+str_A3+"枚です。")


root=tk.Tk()
root.geometry("300x200")

EditBox1= tk.Entry(width=4)
EditBox1.place(x = 250, y = 20)

btn=tk.Button(text="計算する",command=dispLabel).place(x = 60, y = 50)

lbl1=tk.Label(text="調べたい代金はいくらですか?").place(x = 20, y = 20)
lbl2=tk.Label(text=" ")
lbl2.place(x = 20, y = 80)
lbl3=tk.Label(text=" ")
lbl3.place(x = 48, y = 100)
lbl4=tk.Label(text=" ")
lbl4.place(x = 48, y = 120)

tk.mainloop()

お支払い.png
お支払い_入力png.png

感想

ラベル2,3,4のコードを簡易化できないか色々試してみたい。
計算後に表示するラベル3つを、右端に揃える方法を調べたい。

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0