simple_calculator.py
· 1.6 KiB · Python
Bruto
import streamlit as st
# 多國語系字典
LANGUAGES = {
"zh": {
"title": "簡單計算器",
"instruction": "請輸入兩個數字,選擇運算方式:",
"num1": "數字 1",
"num2": "數字 2",
"operation": "選擇運算方式",
"add": "加法 (+)",
"subtract": "減法 (-)",
"calculate": "計算",
"result": "結果:",
"error": "請輸入有效的數字。",
},
"en": {
"title": "Simple Calculator",
"instruction": "Enter two numbers and choose an operation:",
"num1": "Number 1",
"num2": "Number 2",
"operation": "Choose Operation",
"add": "Addition (+)",
"subtract": "Subtraction (-)",
"calculate": "Calculate",
"result": "Result:",
"error": "Please enter valid numbers.",
},
}
# 語言選項
language_options = {"中文": "zh", "English": "en"}
selected_language = st.sidebar.selectbox(
"Select Language", list(language_options.keys())
)
lang = language_options[selected_language]
T = LANGUAGES[lang]
# 顯示標題與說明
st.title(T["title"])
st.write(T["instruction"])
# 取得用戶輸入的數字
num1 = st.text_input(T["num1"])
num2 = st.text_input(T["num2"])
# 運算選項
operation = st.selectbox(
T["operation"], [T["add"], T["subtract"]]
)
# 計算按鈕
if st.button(T["calculate"]):
try:
n1 = float(num1)
n2 = float(num2)
if operation == T["add"]:
result = n1 + n2
else:
result = n1 - n2
st.success(f"{T['result']} {result}")
except ValueError:
st.error(T["error"])
| 1 | import streamlit as st |
| 2 | |
| 3 | # 多國語系字典 |
| 4 | LANGUAGES = { |
| 5 | "zh": { |
| 6 | "title": "簡單計算器", |
| 7 | "instruction": "請輸入兩個數字,選擇運算方式:", |
| 8 | "num1": "數字 1", |
| 9 | "num2": "數字 2", |
| 10 | "operation": "選擇運算方式", |
| 11 | "add": "加法 (+)", |
| 12 | "subtract": "減法 (-)", |
| 13 | "calculate": "計算", |
| 14 | "result": "結果:", |
| 15 | "error": "請輸入有效的數字。", |
| 16 | }, |
| 17 | "en": { |
| 18 | "title": "Simple Calculator", |
| 19 | "instruction": "Enter two numbers and choose an operation:", |
| 20 | "num1": "Number 1", |
| 21 | "num2": "Number 2", |
| 22 | "operation": "Choose Operation", |
| 23 | "add": "Addition (+)", |
| 24 | "subtract": "Subtraction (-)", |
| 25 | "calculate": "Calculate", |
| 26 | "result": "Result:", |
| 27 | "error": "Please enter valid numbers.", |
| 28 | }, |
| 29 | } |
| 30 | |
| 31 | # 語言選項 |
| 32 | language_options = {"中文": "zh", "English": "en"} |
| 33 | selected_language = st.sidebar.selectbox( |
| 34 | "Select Language", list(language_options.keys()) |
| 35 | ) |
| 36 | lang = language_options[selected_language] |
| 37 | T = LANGUAGES[lang] |
| 38 | |
| 39 | # 顯示標題與說明 |
| 40 | st.title(T["title"]) |
| 41 | st.write(T["instruction"]) |
| 42 | |
| 43 | # 取得用戶輸入的數字 |
| 44 | num1 = st.text_input(T["num1"]) |
| 45 | num2 = st.text_input(T["num2"]) |
| 46 | |
| 47 | # 運算選項 |
| 48 | operation = st.selectbox( |
| 49 | T["operation"], [T["add"], T["subtract"]] |
| 50 | ) |
| 51 | |
| 52 | # 計算按鈕 |
| 53 | if st.button(T["calculate"]): |
| 54 | try: |
| 55 | n1 = float(num1) |
| 56 | n2 = float(num2) |
| 57 | if operation == T["add"]: |
| 58 | result = n1 + n2 |
| 59 | else: |
| 60 | result = n1 - n2 |
| 61 | st.success(f"{T['result']} {result}") |
| 62 | except ValueError: |
| 63 | st.error(T["error"]) |
| 64 |