設萬維讀者為首頁 萬維讀者網 -- 全球華人的精神家園 廣告服務 聯繫我們 關於萬維
 
首  頁 新  聞 視  頻 博  客 論  壇 分類廣告 購  物
搜索>> 發表日誌 控制面板 個人相冊 給我留言
幫助 退出
aiwego的博客  
aiwego的博客  
https://blog.creaders.net/u/34231/ > 複製 > 收藏本頁
網絡日誌正文
代碼是實現目的的工具 2024-08-13 04:01:24

下面這段代碼,我覺得還不錯,可以,閱讀起來比較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等等都是高級語言,簡單的代碼,一般人都能閱讀,匯編語言的代碼我現在讀不懂,比高級語言難讀太多了。呵呵呵,加油!!!努力進步!!!!!

瀏覽(743) (0) 評論(0)
發表評論
我的名片
aiwego ,38歲
來自: 中國
註冊日期: 2024-07-30
訪問總量: 41,699 次
點擊查看我的個人資料
Calendar
最新發布
· 夫妻在一起生活,當老婆用測孕試
· 好逸惡勞的女孩子!利用自己的身
· 拜登讓哈里斯黨內接班!把全部給
· 中國年輕的官員,如果最終能登頂
· 國內社交平台上,有網民評論說到
· 胡錦濤時代,主要是70年代末到80
· 馬龍遇到的事情
分類目錄
【個人日誌】
· 夫妻在一起生活,當老婆用測孕試
· 好逸惡勞的女孩子!利用自己的身
· 拜登讓哈里斯黨內接班!把全部給
· 中國年輕的官員,如果最終能登頂
· 國內社交平台上,有網民評論說到
· 胡錦濤時代,主要是70年代末到80
· 馬龍遇到的事情
· 代碼是實現目的的工具
· 我知道大家都喜歡性資源,且性資
· Windows更新窗口中顯示:你的組
存檔目錄
2024-08-05 - 2024-08-14
 
關於本站 | 廣告服務 | 聯繫我們 | 招聘信息 | 網站導航 | 隱私保護
Copyright (C) 1998-2026. Creaders.NET. All Rights Reserved.