简体   繁体   English

我在重新分配python中的变量时遇到问题

[英]I am having problems reassigning a variable in python

I am trying to create a text adventure game in python. 我正在尝试用python创建一个文字冒险游戏。 I am using randint to create a number between 0 and 2 and when I use an if statement to take the random number and assign the biome variable to a biome type it instead takes the original version of the variable and uses that as the biome. 我正在使用randint创建一个介于0和2之间的数字,当我使用if语句获取随机数并将biome变量分配给biome类型时,它将取变量的原始版本并将其用作biome。

#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))

biomes is int value. biomes是int价值。 "0" is string value. "0"是字符串值。

Both values never can be equal. 这两个值永远不能相等。

>>> 0 == "0"
False

Use int literal. 使用int文字。

if biomes == 0:
    biome = 'Forest'
elif biomes == 1:
    biome = 'Taiga'
elif biomes == 2: # else
    biome = 'Mountain'

I recommed you to use random.choice as other suggested. 我建议您使用random.choice作为其他建议。 Simple, eaiser to read. 简单,易于阅读。

>>> random.choice(['Forest', 'Taiga', 'Mountain'])
'Mountain'
>>> random.choice(['Forest', 'Taiga', 'Mountain'])
'Mountain'
>>> random.choice(['Forest', 'Taiga', 'Mountain'])
'Taiga'

random.randint(...) returns an integer. random.randint(...)返回一个整数。 You are comparing the value to a string here. 您正在将值与字符串进行比较。

>>> type(randint(0, 2))
<type 'int'>

Your if statements should be rewritten as - 您的if语句应重写为-

if biomes == 0:
    biome='Forest'
elif biomes == 1:
    biome='Taiga'
else:
    biome='Mountain'

PS -- You don't need three if statements, since, if the value is 0 , it can never be 1 or 2 , so no need to check the conditions. PS-您不需要三个if语句,因为如果值为0 ,则永远不能为12 ,因此无需检查条件。 You can use an if-elif-else construct instead. 您可以改用if-elif-else构造。

Here's a more understandable way: 这是一种更容易理解的方式:

from random import choice
biomes = ['Forest', 'Tiaga', 'Mountain']
biome = choice(biomes)

Then if the number of biomes increases, or decreases, you don't have to worry about updating the range for the random number, and not quite getting your if statements right... 然后,如果生物群落数量增加或减少,则不必担心更新随机数的范围,也不必担心if语句正确无误。

You need to compare to 0 rather than the string value "0" as in if biome == 0: 您需要比较0而不是字符串值"0"if biome == 0:

However, this can be simplified with random.choice to select a biome randomly from a list. 但是,可以使用random.choice从列表中随机选择一个生物群系进行简化。

biome = random.choice(['Forest', 'Taiga', 'Mountain'])

and eliminate your if s altogether. 并消除你的if完全秒。

我认为您应该尝试使用if语句biome == 1代替biome ==“ 1”,并且对biome == 2和biome == 3相同。

That's because you are comparing an integer value to a string (which will never equal). 这是因为您正在将整数值与字符串(永远不会相等)进行比较。 This will work: 这将起作用:

import random
trees=random.randint(0,50)
biomes=random.randint(0,2)
animals=random.randint(0,3)
wolves=random.randint(0,5)

# Compare biomes (which is an integer) to another integer
if biomes == 0:
    biome='Forest'

# Use elif for multiple comparisons like that
elif biomes == 1:
    biome='Taiga'

# Use an else here because the only other option is 2
else:
    biome='Mountain'

print ('''You are in a %s biome.'''%(biome))

Note also I removed the first and second lines of your script. 另请注意,我删除了脚本的第一行和第二行。 The first would have blown up because biome isn't defined yet and the second does nothing. 第一个因为没有定义biome被炸毁,而第二个则什么也没做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM