【发布时间】:2022-11-10 22:06:36
【问题描述】:
我有多个变量要传入 if、elif 和 else 语句。假设 3 个变量 a、b 和 c。这些只是包含数字的列表。但是我需要为变量的每个概率定义 if、elif 和 else 语句。
例如:
- 如果其中一个变量 >0 对该变量执行某些操作,但传递其他变量。
基于概率,我知道所有的可能性,因此我根据这些可能性准备代码
weeks =9
a=[1,0,1,1,1,0,0,0,1]
b=[1,0,0,1,0,1,1,0,1]
c=[1,0,0,0,1,0,1,1,1]
for i in range (weeks):
if i <= 0:
(print('this is hypo'))
else:
if(a[i] <= 0 and b[i] <= 0 and c[i] <= 0): # no prod 0
print(a[i],b[i],c[i],'no ne is working')
elif(a[i] > 0 and b[i] <= 0 and c[i] <= 0): # only first 1
print(a[i],b[i],c[i],'only a working')
elif(a[i] > 0 and b[i] > 0 and c[i] <= 0): #first and second 1-2
print(a[i],b[i],c[i],'a and b working')
elif(a[i] > 0 and b[i] <= 0 and c[i] > 0): # first and third 1-3
print(a[i],b[i],c[i], 'a and c working')
elif(a[i] <= 0 and b[i] > 0 and c[i] <= 0): # only second 2
print(a[i],b[i],c[i],'only b working')
elif(a[i]<= 0 and b[i] > 0 and c[i] > 0): #second and third 2-3
print(a[i],b[i],c[i],'b and c working')
elif(a[i] <= 0 and b[i] <= 0 and c[i] > 0): # only third 3
print(a[i],b[i],c[i],'only c working')
else: # all of are working 1-2-3
print (a[i],b[i],c[i], 'all wokring')
print('iteration number :',i)
我想要实现的是找到一种有效的方法来在几句话中传递这些可能性。处理 3 个变量并不是什么大问题,但是如果我想传递 10 个变量会发生什么。我需要分别定义每个概率吗?
【问题讨论】:
标签: python python-3.x if-statement