我想在python中創建一個文本冒險遊戲。我正在使用randint創建一個介於0和2之間的數字,當我使用if語句獲取隨機數並將生物羣變量分配給生物羣類型時,它將使用原始版本的變量並將其用作生物羣。我有問題重新分配python中的變量
#Defines game()
print ('''You are in a %s biome.'''%(biome))
biome='placeholder'
import random
trees=random.randint(0,50)
biomes=random.randint(0,2)
animals=random.randint(0,3)
wolves=random.randint(0,5)
if biomes == "0":
biome='Forest'
if biomes == "1":
biome='Taiga'
if biomes == "2":
biome='Mountain'
print ('''You are in a %s biome.'''%(biome))
您的問題的答案是正確的,但是Python使[[隨機]更容易。](http://docs.python.org/2/library/random.html#random.choice):'biome = random.choice(['Forest','Taiga','Mountain'])' – kevingessner
@ user2438758,[接受答案](http://stackoverflow.com/help/accepted-answer)。 – falsetru