簡體   English   中英

如何打印我的函數數學結果? [python 函數](已編輯)

[英]How do I print the results of my math of my functions? [python functions] (edited)

我有 3 個不同的小程序,我不知道如何顯示它們,我已經完成了數學和東西,我真的不知道還能說什么,除了我需要絕望的幫助,因為我沒有人參與我在這個主題上經歷過的生活可以幫助我。

  1. 這個使用 function 來確定是否有任何年份是閏年。 使您的 function 返回 boolean 值。 (閏年可以被 4 整除,除非它們發生在不能被 400 整除的新世紀之初。1900 年不是閏年;2000 年是。)
  2. 一個程序,它接受一條線上的兩個點,並在方程 y = m x + b 中返回 m 和 b 的值。 (m = (y1 - y2) / (x1 - x2) 和 b = y1 - m x1)
  3. 在子程序中使用歐幾里得算法來找到兩個數的最大公因數的程序。 示例:45 和 55 的 GCF 為 5,從序列:55、45、10、5、0。返回 GCF

編碼為 1。

def isLeapYear(year):
    if year % 4 == 0:
        if year % 100 == 0:
            if year % 400 == 0:
                return True
            else:
                return False
        else:
            return True
    else:
        return False

編碼為 2。

    rise = y2-y1
    run = x2-x1
    m = rise/run
    b = y2/(m*x2)
    return print("m = " + str(m) + " and b = " + str(b))

編碼為 3。

def gcf(n1,n2):

    remainder = None

    while remainder != 0:
        remainder = n1 % n2
        n1 = n2
        n2 = remainder


    return n1

編輯:對不起,我不擅長解釋事情,例如,我想要閏年到 function 之類的; “輸入你的閏年”(stores function)對不起,但是(leapyeartheyentered)不是閏年! (或)(他們輸入的閏年)是閏年!

是的,有人說它是因為我的英語也很差,我想-我想打印編碼的結果

聽起來您正在解決一個家庭作業問題,並且該問題要求您編寫函數來完成三件不同的事情。 要求您圍繞它們編寫包裝代碼來調用函數並打印出結果,但您可能出於自己的原因想要這樣做(例如,在提交作業之前測試函數,或者只是為了學習如何)。

無論如何,要學習的關鍵是如何調用函數。 對於閏年 function,您可能希望在if語句中進行調用,因為您將根據返回的 Boolean 結果打印兩條不同的消息。

year = int(input("Enter your leap year!")) # prompt the user to enter a year, convert to int

if isLeapYear(year):
    print(year, "is a leap year!")
else:
    print("I'm sorry,", year, "is not a leap year.")

對於您的第二個 function,您可能需要更改其代碼以return mb值,並將打印留給調用代碼,因為這就是問題陳述所說的。 在 Python 中,您可以返回一個值元組,這在大多數情況下就像一次返回兩個值一樣(您可以非常輕松地打包和解包元組)。 你切斷了第二個 function 的名稱,但如果我們將其命名為calcLine ,則更新后的 function 和調用代碼可能如下所示:

def calcLine(x1, y1, x2, y2):
    rise = y2-y1
    run = x2-x1
    m = rise/run
    b = y2 - (m*x2) # math fix, as suggested in the comments above
    return m, b  # return a 2-tuple, rather than printing here

coords_string = input("Enter x1, y1, x2, y2 coordinates: ") # get a string of coordinates
x1, y1, x2, y2 = map(float, coords_string.split(','))       # parse the string into numbers

slope, intercept = calcLine(x1, y1, x2, y2)                 # do the calculation

print("m = {} and b = {}".format(slope, intercept))         # print our results

如果您不完全理解調用代碼的字符串解析和格式化部分,請不要擔心,您要理解的關鍵行是 function 調用: m, b = calcLine(x1, y1, x2, y2) 這將調用上面定義的 function,並將返回的mb值保存在一個元組中,並將它們解壓縮到兩個新的全局變量中,我們以后可以使用(我選擇使用與mb不同的名稱來制作它清楚地表明它們與函數的變量是分開的,認為它們具有相同的值)。

最后一個 function 可能是最容易處理的,但既然你還沒有說你想打印什么,我就把它留給你!

對於第一個問題,請使用:

y = int(input('Enter year: '))
if isLeapYear(y):
  print('%d is a leap year' % y)
else:
  print('%d is not a leap year' % y)

在第二個問題中:

return print("m = " + str(m) + " and b = " + str(b))

返回None 那是因為 function print返回None 它用於打印。

您可能想創建一個返回某些內容的 function,或者您想打印一些內容。

如果您需要從 function 返回兩件事,您可以:

return m,b

然后在調用 function 的代碼中:

m,b = CalcMB(x1,y1,x2,y2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM