我遇到了一个挑战,我决定在这里问。 看我用下面的代码是为了解释一下吧:
x=y=z=2
def function1(x):
return 2+x
def function2(y):
return 4+y
def function3(z):
return 6 +z
use_1 = input("Type T to use 1 ")
use_2 = input("Type T to use 2 ")
use_3 = input("Type T to use 3 ")
h=(use_1=='T')*function1(x)+(use_2=='T')*function2(y)+(use_3=='T')*function3(z)
print(h)
****说明:
由于这个原因,用户决定使用这些函数中的哪一个。用户可能希望使用1和3或2和3或1和2,甚至1或2或3。可以通过选择T
现在我有一个很大的挑战:
我写下面的代码只是为了一个问题。我们可以看到,如果用户选择了函数1
,那么它的参数将在代码中使用,但是如果用户没有选择函数3
,那么它的参数e
和q
应该在代码的其余部分“每个e
存在的地方”中删除。我们通过删除和修改代码来手动完成。
当用户选择1
而不选择其他函数时,有什么方法仅仅是a
和q
在代码中?
我的意思是所有e
,c
都会自动删除
我希望能很好地理解我的意见
ao = np.zeros((N,))
ao[0] = 0.3
an = np.zeros((N,))
q0o = np.zeros((N,))
qo[0] = 0.3
q0n = np.zeros((N,))
c0o = np.zeros((N,))
c0o[0] = 69
c0n = np.zeros((N,))
eo = np.zeros((N,))
eo[0] = 0.1
en = np.zeros((N,))
temp = 1e10
def 1(a,q):
return a+q**2
def 2(c,q):
return c/sqrt(q)
def 3(e,q):
return q-e
def TOTAL(a, c, e, q) :
total = 1(a,q) + 2(c,q) + 3(e,q)
return total
highest=0
with open('outputs/poly.txt', 'w') as f:
for i in range (1,N):
num = 0
R = np.random.uniform(0,1)
while True:
num += 1
an[i] = ao[i-1] + 0.001 * np.random.normal()
c0n[i] = c0o[i-1] + 0.01 * np.random.normal()
e0n[i] = e0o[i-1] + 0.01 * np.random.normal()
L = np.exp(-0.5 * (TOTAL(an[i], c0n[i], e0n[i], qn[i]) - TOTAL(ao[i-1], c0o[i-1], e0o[i-1], qo[i-1])))
LL=min(1,max(L,0))
if LL>R:
ao[i]= an[i]
c0o[i] = c0n[i]
e0o[i] = e0n[i]
qo[i] = qn[i]
chi = TOTAL(ao[i], c0o[i], e0o[i], qo[i])
else:
ao[i]= ao[i-1]
c0o[i] = co[i-1]
e0o[i] = e0o[i-1]
qo[i] = qo[i-1]
chi = TOTAL(ao[i], c0o[i], e0o[i], qo[i])
if (ao[i]>0 and c0o[i]>0 and e0o[i]>0) or num>100:
highest = max(num, highest)
break
f.write("{0}\t{1}\t{2}\t{3}\t{4}\n".format(round(chi,5),' ',round(c0o[i],5),' ',round(qo[i],5)))
目前没有回答
相关问题 更多 >
编程相关推荐