[英]How to use the value of a function within the same function
I am trying to create a loop that will randomly generate numbers until reaching some condition (eg > 16).我正在尝试创建一个循环,该循环将随机生成数字,直到达到某种条件(例如> 16)。 How do I use a value, obtained using a function I created, within that same function?
如何在同一个 function 中使用我创建的 function 获得的值? The only options I can think of are using 'return' and 'print' but they don't seem to work.
我能想到的唯一选择是使用“返回”和“打印”,但它们似乎不起作用。
Here's an example of what I've come up with so far:这是我到目前为止提出的一个示例:
>>> def y():
... x = random.randint(1,24)
... print(x) (I've also tried 'return x' at this line)
... while int(x) < 16:
... return x
Use a generator:使用生成器:
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.