亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

函數參數:ab 未定義

函數參數:ab 未定義

慕虎7371278 2021-12-29 10:45:41
當我運行這段代碼時,它說ab沒有定義。問題是什么?import mathdef taylor(ab):    ab = float(input("What is the parameter precision? :"))    print(f"Calling taylor: {(ab)}")    num = 0    x = 1    n = 0    y = 1    while abs(math.pi - num) > ab:        num = num + (4 * (x / y))        x = x * -1        y += 2        n = n + 1    print(f"Calling basel : {(ab)} returns {(num), (n)}")taylor(ab)
查看完整描述

2 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

import math

def taylor():

    ab = float(input("What is the parameter precision? :"))

    print(f"Calling taylor: {(ab)}")

    num = 0

    x = 1

    n = 0

    y = 1

    while abs(math.pi - num) > ab:

        num = num + (4 * (x / y))

        x = x * -1

        y += 2

        n = n + 1

    print(f"Calling basel : {(ab)} returns {(num), (n)}")

taylor()

或者:


import math

def taylor(ab):


    print(f"Calling taylor: {(ab)}")

    num = 0

    x = 1

    n = 0

    y = 1

    while abs(math.pi - num) > ab:

        num = num + (4 * (x / y))

        x = x * -1

        y += 2

        n = n + 1

    print(f"Calling basel : {(ab)} returns {(num), (n)}")

ab = float(input("What is the parameter precision? :"))

taylor(ab)


查看完整回答
反對 回復 2021-12-29
?
慕田峪4524236

TA貢獻1875條經驗 獲得超5個贊

您試圖在定義變量ab之前使用它。您不能將未定義的變量傳遞給函數。在taylor()不需要聲明任何參數。刪除參數應該可以解決您的問題:


import math


def taylor():

    ab = float(input("What is the parameter precision? :"))

    print(f"Calling taylor: {(ab)}")

    num = 0

    x = 1

    n = 0

    y = 1

    while abs(math.pi - num) > ab:

        num = num + (4 * (x / y))

        x = x * -1

        y += 2

        n = n + 1

    print(f"Calling basel : {(ab)} returns {(num), (n)}")


taylor()


查看完整回答
反對 回復 2021-12-29
  • 2 回答
  • 0 關注
  • 173 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號