定義具有以下規格的函式。
接受兩個引數: x:一個縮放器 l:一個串列 該函式將檢查 x 是否存在于串列中,如果存在則列印結果:是 如果不存在:否 例如:
func1(x = 12, l = [1,45,5,3]) 將產生
12 是沒有
def inList(x=12,l=[1,45,5,3]):
for x in fuc1:
if x in 1:
print("value is in the list")
else:
print("value is not in the list")
uj5u.com熱心網友回復:
這可以是一個簡單的 if 陳述句
If x in i:
print("It is here")
else:
print("It is not here")
uj5u.com熱心網友回復:
一樣簡單:
def is_in_list(x, l):
if x in l:
print('in list')
return
else:
print('not in list')
如果你不能使用in或者你試圖使用回圈來做,那么你可以這樣做:
def is_in_list(x, l):
for item in l:
if(item == x):
print('in list')
return
print('not in list')
uj5u.com熱心網友回復:
你可以做
["No", "Yes"][x in l]
但是,正如@chepner 評論的那樣,您似乎應該撰寫自己的實作in,因此您應該首先避免使用它。
如何查看每個專案l并測驗是否與您的目標相同。你知道如何測驗兩個專案是否具有相同的價值嗎?
(順便說一句,避免l用作名稱。在許多編輯器中它看起來像數字 1)
uj5u.com熱心網友回復:
您錯過了變數名稱。此外,在這種情況下不需要 for 回圈。
def inList(x=12,l=[1,45,5,3]):
if x in l:
print("value is in the list")
else:
print("value is not in the list")
uj5u.com熱心網友回復:
我希望這能幫到您:
MyList = [1,45,5,3]
x=12
def my_function(MyList , x):
# Print list
print("Our List: ", MyList )
# Check if 'b' exists in the list or not
if x in MyList:
print(" value is in the list")
else:
print(" value is not in the list")
my_function(MyList, x)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/519550.html
標籤:Python功能
下一篇:將多個專案添加到字典
