我试图制作一个超级基本的进化模拟器来生成十个随机生物"这些都是一个数字值然后给他们一个随机的"突变"但它一直在向我抛出这个错误:"对于我在范围内(生物): TypeError:'元组' object不能解释为整数"
import random
from random import randint
creatures = (random.randint(1, 10), random.randint(1, 10))
print(creatures)
for i in range(creatures):
mutation = random.randint(1, 2)
newEvolution = creatures[i] + mutation
print("New evolution", newEvolution)
答案 0 :(得分:3)
生物是tuple
,range
正在寻找整数。要迭代元组,只需执行:
for c in creatures:
mutation = random.randint(1, 2)
newEvolution = c + mutation
答案 1 :(得分:0)
你需要迭代生物长度的范围。
from random import randint
creatures = (random.randint(1, 10), random.randint(1, 10))
print(creatures)
for i in range(len(creatures)): # iterate over the range of the lenght of the creatures
mutation = random.randint(1, 2)
newEvolution = creatures[i] + mutation
print("New evolution", newEvolution)
答案 2 :(得分:0)
生物是一个元组和range()函数,因为参数需要整数。
Syntax for range:
range([start], stop[, step])
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step: Difference between each number in the sequence.
Note that:
All parameters must be integers.
解决方案:
import random
creatures = random.sample(range(1, 11), 10)
print(creatures)
newEvolution = []
for i in range(len(creatures)):
mutation = random.randint(1, 2)
newEvolution.append(creatures[i] + mutation)
print("New evolution", newEvolution)