[英]How to use the value of a function within the same function
我正在嘗試創建一個循環,該循環將隨機生成數字,直到達到某種條件(例如> 16)。 如何在同一個 function 中使用我創建的 function 獲得的值? 我能想到的唯一選擇是使用“返回”和“打印”,但它們似乎不起作用。
這是我到目前為止提出的一個示例:
>>> def y():
... x = random.randint(1,24)
... print(x) (I've also tried 'return x' at this line)
... while int(x) < 16:
... return x
使用生成器:
def y():
while True:
x = random.randint(1, 24)
if x >= 16:
return
yield x
for i in y():
print(i)
# Output:
1
14
10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.