- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在创建一个游戏,总体上对 Python 还很陌生。
我创建了一个函数“descriptionGenerator()”,它随机或使用传递给它的变量生成角色和对象的描述。
它似乎工作正常,但有时它无法正常工作。所以我把它放在一个循环中,如果没有一个迭代出现这个问题,它似乎永远无法完成循环。
代码如下:
#+------------------------------------------+
#| Name: bitsandpieces.py |
#| A module for the 'Europa I' game |
#| created for the Game Making Competition |
#| |
#| Date Created/Modified: |
#| 3/4/10 | 3/4/10 |
#+------------------------------------------+
# Import the required modules
# Import system modules:
import time
import random
# Import 3rd party modules:
# Import game modules:
# Define the 'descriptionGenerator()' function
def descriptionGenerator(descriptionVariables):
descriptionVariableSize = len(descriptionVariables)
if descriptionVariables[0] == 'char':
# If there is only one variable ('char'), create a random description
if descriptionVariableSize == 1:
# Define choices for descriptionVariables to be generated from
gender_choices = ['male', 'female']
hair_choices = ['black', 'red', 'blonde', 'grey', 'brown', 'blue']
hair_choices2 = ['long', 'short', 'cropped', 'curly']
size_choices = ['tubby', 'thin', 'fat', 'almost twig-like']
demeanour_choices = ['glowering', 'bright', 'smiling', 'sombre', 'intelligent']
impression_choices = ['likeable', 'unlikeable', 'dangerous', 'annoying', 'afraid']
# Define description variables
gender = random.choice(gender_choices)
height = str(float('0.' + str(random.randint(1, 9))) + float(random.randint(1, 2)))
if float(height) > 1.8:
height_string = 'tall'
if float(height) > 2:
height_string = 'very tall'
elif float(height) < 1.8 and float(height) > 1.5:
height_string = 'average'
elif float(height) < 1.5:
height_string = 'short'
if float(height) < 1.3:
height_string = 'very short'
hair = random.choice(hair_choices2) + ' ' + random.choice(hair_choices)
size = random.choice(size_choices)
demeanour = random.choice(demeanour_choices)
impression = random.choice(impression_choices)
# Collect description variables in list 'randomDescriptionVariables'
randomDescriptionVariables = ['char', gender, height, height_string, hair, size, demeanour, impression]
# Generate description using the 'descriptionGenerator' function
descriptionGenerator(randomDescriptionVariables)
# Generate the description of a character using the variables passed to the function
elif descriptionVariableSize == 8:
if descriptionVariables[1] == 'male':
if descriptionVariables[7] != 'afraid':
print """A %s man, about %s m tall. He has %s hair and is %s. He is %s and you get the impression that he is %s.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
elif descriptionVariables[7] == 'afraid':
print """A %s man, about %s m tall. He has %s hair and is %s. He is %s.\nYou feel that you should be %s of him.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
elif descriptionVariables[1] == 'female':
if descriptionVariables[7] != 'afraid':
print """A %s woman, about %s m tall. She has %s hair and is %s. She is %s and you get the impression that she is %s.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
elif descriptionVariables[7] == 'afraid':
print """A %s woman, about %s m tall. She has %s hair and is %s. She is %s.\nYou feel that you should be %s of her.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
else:
pass
elif descriptionVariables[0] == 'obj':
# Insert code here 2 deal with object stuff
pass
print
print
myDescriptionVariables = ['char']
i = 0
while i < 30:
print
print
print
descriptionGenerator(myDescriptionVariables)
i = i + 1
time.sleep(10)
当它无法正确执行时,它会这样说:
Traceback (most recent call last):
File "/Users/Jasper/Development/Programming/MyProjects/Game Making Challenge/Europa I/Code/Code 2.0/bitsandpieces.py", line 79, in <module>
descriptionGenerator(myDescriptionVariables)
File "/Users/Jasper/Development/Programming/MyProjects/Game Making Challenge/Europa I/Code/Code 2.0/bitsandpieces.py", line 50, in descriptionGenerator
randomDescriptionVariables = ['char', gender, height, height_string, hair, size, demeanour, impression]
UnboundLocalError: local variable 'height_string' referenced before assignment
感谢您对此的任何帮助
-----编辑-----
感谢您的帮助,解决了那个问题,但现在还有另一个问题!
我更改了这段代码 2 将字符串作为变量,然后“返回”它们,然后测试它们:
elif descriptionVariableSize == 8:
if descriptionVariables[1] == 'male':
if descriptionVariables[7] != 'afraid':
descriptionOutput = """A %s man, about %s m tall. He has %s hair and is %s. He is %s and you get the impression that he is %s.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
return descriptionOutput
elif descriptionVariables[7] == 'afraid':
descriptionOutput = """A %s man, about %s m tall. He has %s hair and is %s. He is %s.\nYou feel that you should be %s of him.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
return descriptionOutput
elif descriptionVariables[1] == 'female':
if descriptionVariables[7] != 'afraid':
descriptionOutput = """A %s woman, about %s m tall. She has %s hair and is %s. She is %s and you get the impression that she is %s.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
return descriptionOutput
elif descriptionVariables[7] == 'afraid':
descriptionOutput = """A %s woman, about %s m tall. She has %s hair and is %s. She is %s.\nYou feel that you should be %s of her.""" %(descriptionVariables[3], descriptionVariables[2], descriptionVariables[4], descriptionVariables[5], descriptionVariables[6], descriptionVariables[7])
return descriptionOutput
else:
print 'Incorrect number of variables contained within \'descriptionVariables\''
elif descriptionVariables[0] == 'obj':
# Insert code here 2 deal with object stuff
pass
myDescriptionVariables = ['char']
myOutput = descriptionGenerator(myDescriptionVariables)
print myOutput
但是,当我运行它时,它会给出以下输出:
None
我做错了什么?
最佳答案
您在 if/else 语句中定义 height_string:
if float(height) > 1.8:
height_string = 'tall'
if float(height) > 2:
height_string = 'very tall'
elif float(height) < 1.8 and float(height) > 1.5:
height_string = 'average'
elif float(height) < 1.5:
height_string = 'short'
if float(height) < 1.3:
height_string = 'very short'
但是,如果 height == 1.8 或 height == 1.5,则所有 if/elif 语句都为假,因此永远不会定义 height_string。只需将第二个语句更改为具有 <= 和 >= 符号即可:
elif float(height) <= 1.8 and float(height) >= 1.5:
响应您的编辑:
你用
调用函数myOutput = descriptionGenerator(["char"])
请注意,您传递的是一个长度为 1 的列表。因此,您的函数会看到 descriptionVariableSize == 1
并创建随机描述。
到目前为止,还不错。
但是等等!在该 if 语句的末尾,您有一个 elif:
elif descriptionVariableSize == 8:
现在您确实有 descriptionVariableSize == 8。但是,您使用了 elif - 所以您只是花了所有时间来创建一组随机描述,但您永远不会使用它,因为第一个陈述是正确的,而这个陈述是仅在第一条语句为假时执行。
解决方案 - 只需将 elif
更改为 if
。现在,无论您是传入一个完整的语句还是在函数中生成一个语句,您仍然会到达第二部分。
编辑...第五个?
我没有注意到你在随机部分的末尾再次调用了该函数。请注意,您不会返回
该调用 - 因此返回的任何内容都将丢失。只需将其更改为
return descriptionGenerator(randomDescriptionVariables)
作为附加说明 - 您的函数变得有点笨拙。问题是您在函数中做了两件完全不同的事情——一件是生成随机的品质列表,另一件是根据这些品质生成描述。最好将第一个 if
block 中的内容移动到它自己的函数中,generateRandomDescription()
或其他仅在第一个 if 中调用的函数
block 。
关于python - python错误-不明白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2570023/
我是一名优秀的程序员,十分优秀!