下面這段代碼,我覺得還不錯,可以,閱讀起來比較happy輕鬆容易
def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): if y != 0: return x / y else: return "錯誤:被除數不能為零" while True: print("選項:") print("1.加") print("2.減") print("3.乘") print("4.除") print("5.退出") choice = input("鍵入選項(1-5): ") if choice == '5': break num1 = float(input("輸入第一個數字:")) num2 = float(input("輸入第二個數字:")) if choice == '1': print("Result:", add(num1, num2)) elif choice == '2': print("Result:", subtract(num1, num2)) elif choice == '3': print("Result:", multiply(num1, num2)) elif choice == '4': print("Result:", divide(num1, num2)) else: print("無效的輸入") 再下面這段代碼,也比較容易閱讀,比較簡單,但我覺得沒上面那段代碼happy def add(x, y): return x + y def subtract(x, y): return x - y def multiply(x, y): return x * y def divide(x, y): return x / y print("選擇操作:") print("1. 相加") print("2. 相減") print("3. 相乘") print("4. 相除") choice = input("輸入你的選擇(1/2/3/4): ") num1 = float(input("輸入第一個數字: ")) num2 = float(input("輸入第二個數字: ")) if choice == '1': print(num1, "+", num2, "=", add(num1, num2)) elif choice == '2': print(num1, "-", num2, "=", subtract(num1, num2)) elif choice == '3': print(num1, "*", num2, "=", multiply(num1, num2)) elif choice == '4': print(num1, "/", num2, "=", divide(num1, num2)) else: print("無效的輸入") c c++ c# Java Python delphi Pascal perl等等都是高級語言,簡單的代碼,一般人都能閱讀,匯編語言的代碼我現在讀不懂,比高級語言難讀太多了。呵呵呵,加油!!!努力進步!!!!! |