timmy hat die Gist bearbeitet 9 months ago. Zu Änderung gehen
1 file changed, 63 insertions
simple_calculator.py(Datei erstellt)
| @@ -0,0 +1,63 @@ | |||
| 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"]) | |
Neuer
Älter