在這個網站上有類似的問題,但我發現答案只適用於字符串輸出。例如截然不同的事情。想象一下,如果我有這個python程序:定義一個名爲avg4的函數。返回四個數字的平均值
#!/usr/bin/env python
def printAndReturnNothing():
x = "hello"
print(x)
def printAndReturn():
x = "hello"
print(x)
return x
def main():`enter code here`
ret = printAndReturn()
other = printAndReturnNothing()
print("ret is: %s" % ret)
print("other is: %s" % other)
if __name__ == "__main__":
main()
你認爲什麼是輸出?
hello
hello
ret is : hello
other is: None
但是,問題是要定義一個叫做avg4
的函數。它要求用戶輸入四個數字並返回四個數字的平均值。第二個問題要求定義一個叫做avg
的函數。它要求用戶輸入三個數字並打印平均值。
難道這些是不一樣的輸出嗎?
這是我avg4
代碼:
def avg4(a,b,c,d):
a=int(input(ënter a number")
b
c
d
avg=a+b+c+d/4
return
當我喊它,它會提示用戶輸入四個數字,但不返回任何東西。而第二個,avg
將打印平均值。