Python 我的while循环不会随机化列表,而是开始重复列表中的相同单词
我创建了一个while循环,希望它能将列表随机化,但相反,它开始重复相同的动物和相同的水果Python 我的while循环不会随机化列表,而是开始重复列表中的相同单词,python,Python,我创建了一个while循环,希望它能将列表随机化,但相反,它开始重复相同的动物和相同的水果 import time import random animals = ['sheep','cow','cat','dog'] fruits = ['oranges','apples', 'banana'] ani = random.choice(animals) fru = random.choice(fruits) while True: print(ani+fru) 如果每次都要更改
import time
import random
animals = ['sheep','cow','cat','dog']
fruits = ['oranges','apples', 'banana']
ani = random.choice(animals)
fru = random.choice(fruits)
while True:
print(ani+fru)
如果每次都要更改随机函数,则必须将其放入循环中 试试这个: "
“请从下一页重复和。堆栈溢出不是为了替换现有的教程和文档。random.choice需要在循环体中,因为您编写了一个程序来选择一个随机动物和一个随机水果,然后永远打印这一组合。请重复有关循环的教程。
animals = ['sheep','cow','cat','dog']
fruits = ['oranges','apples', 'banana']
while True:
ani = random.choice(animals)
fru = random.choice(fruits)
print(ani+fru)