我以国家为例,但是我想知道是否需要将国家合并为一个变量,还是需要做其他事情?
import random
afghanistan = 189
albania = 189
algeria = 189
andorra = 189
if albania == afghanistan or albania or algeria or andorra:
albania = random.randint(1, 196)
print(afghanistan)
print(albania)
print(algeria)
print(andorra)
答案 0 :(得分:0)
我在这里对您的目标进行一些假设,但您的情况目前并未将albania
与其他值进行比较。
它检查albania
等于afghanistan
,还是albania
,algeria
或andorra
不为0。
您应该为每一个创建一个单独的条件:
import random
afghanistan = 189
albania = 189
algeria = 189
andorra = 189
if albania == afghanistan or albania == algeria or albania == andorra:
albania = random.randint(1, 196)
print(afghanistan)
print(albania)
print(algeria)
print(andorra)
答案 1 :(得分:0)
尚不完全清楚您要实现的目标,但我认为唯一合理的目标是以随机方式找到这些国家/地区的ID,您可以这样实现:
import random
afghanistan = 189
albania = 189
algeria = 189
andorra = 189
countries = [afghanistan, albania, algeria, andorra]
for index, country in enumerate(countries):
while country in (countries[:index] + countries[index+1:]):
country = random.randint(1, 196)
countries[index] = country
print(countries) #example output (will be different each time): [170, 32, 14, 189]