0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ミニプロジェクト1:電卓アプリを作る【Day 23】

Last updated at Posted at 2025-12-22

Qiita Advent Calendar 2025 のパイソニスタの一人アドカレ Day23 の記事です。

ミニプロジェクト1:電卓アプリを作る

Python の基礎を学んだら、自分で動く簡単なアプリを作ってみましょう。
今日は 四則演算ができる電卓アプリ を作ります。

1. 基本の考え方

やりたいこと:

  1. ユーザーから数字と演算子を入力してもらう
  2. 入力に応じて計算する
  3. 結果を表示する
  4. 終了まで繰り返す

2. サンプルコード

def calculator():
    while True:
        num1 = input("1つ目の数字を入力してください(終了は q): ")
        if num1.lower() == "q":
            break
        op = input("演算子を入力してください (+, -, *, /): ")
        num2 = input("2つ目の数字を入力してください: ")

        try:
            num1 = float(num1)
            num2 = float(num2)
            if op == "+":
                result = num1 + num2
            elif op == "-":
                result = num1 - num2
            elif op == "*":
                result = num1 * num2
            elif op == "/":
                result = num1 / num2
            else:
                print("演算子が正しくありません")
                continue
            print("結果:", result)
        except ValueError:
            print("数字を正しく入力してください")
        except ZeroDivisionError:
            print("0で割ることはできません")

calculator()

3. ポイント解説

  • while ループ:ユーザーが終了するまで繰り返す

  • input():ユーザーから入力を取得

  • float():小数点も扱えるように変換

  • try-except:エラー処理

    • ValueError:数字以外の入力
    • ZeroDivisionError:0で割ろうとした場合
  • if-elif-else:演算子に応じて処理を分ける

4. 改良アイデア

  • 入力をまとめて "3 + 5" のように処理する
  • 計算履歴をリストに保存して後で表示する
  • 小数点以下の表示桁数を調整する

5. まとめ

  • 基本的な Python の制御構造を総復習できる
  • ユーザー入力、条件分岐、ループ、例外処理を組み合わせる練習になる
  • 自分で改良して楽しむことで理解が深まる
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?