编程语言
首页 > 编程语言> > 如何创建一个在python中给出答案时关闭的循环?

如何创建一个在python中给出答案时关闭的循环?

作者:互联网

我想做的是随机生成两个等于给定数字的数字.但是,为了让它获得所需的答案,我希望它是随机的.那就是问题所在.

a=(1,2,3,4,5,6,7,8,9,)
from random import choice
b=choice(a)
c=choice(b)
d= c+b
if d == 10:
#then run the rest of the program with the value's c and b in it
#probably something like sys.exit goes here but I am not sure to end \/
else:
# i have tryied a few things here but I am not sure what will loop it around*

(感谢帮助:D)

我知道创建了一个名为“正确”的列表,并且知道尝试将值a和b附加到该列表中,但这是行不通的.因为我知道运行程序’in range for range(100)’,所以我得到了答案.但是,这些值未添加到新列表权限中.这是问题所在.然后我要做的是从列表中正确读取值0和1,然后使用它们(对不起,在学校做得不好)
这是针对未添加到给定变量的分数.这一点,但是是.

import sys
right=(0)
y=x+x
from trail in range(y)
a=(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20)
from random import choice 
A=choice(a)
B=choice(a)
d=A/B
if d==1:
    right.append(A)
    right.append(B)
else:
    x.append(1)

解决方法:

 from random import choice

 a = range(1, 10)
 b = c = 0
 while b + c != 10:
     b = choice(a)
     c = choice(a)  # You meant choice(a) here, right?

但这完成了同一件事:

 b = choice(a)
 c = 10 - b

对于介于0和10之间的十进制数字:

from random import uniform

b = uniform(0, 10)
c = 10 - b

标签:flow-control,loops,python
来源: https://codeday.me/bug/20191101/1981759.html