我正在创建一款游戏,而且对于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
我做错了什么?
答案 0 :(得分:3)
在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语句都为false,因此永远不会定义height_string。只需将第二个语句更改为&lt; =和&gt; = sign:
elif float(height) <= 1.8 and float(height) >= 1.5:
回复您的修改:
您使用
调用该函数myOutput = descriptionGenerator(["char"])
请注意,您传递的是长度为一的列表。因此,您的函数会看到descriptionVariableSize == 1
并创建随机说明。
到目前为止,非常好。
但是等等!在if语句结束时,你有一个elif:
elif descriptionVariableSize == 8:
现在你有了descriptionVariableSize == 8.但是,你使用elif - 所以你只花了所有时间创建一组随机的描述,但你永远不会使用它,因为第一个语句是真的,这句话是仅在第一个语句为假时执行。
解决方案 - 只需将elif
更改为if
即可。现在,无论你传入一个完整的陈述还是在函数中生成一个陈述,你仍然会到达第二部分。
编辑...第五个?
我没注意到你在随机部分的末尾再次调用该函数。请注意,您没有return
该调用 - 因此返回的任何内容都会丢失。只需将其更改为
return descriptionGenerator(randomDescriptionVariables)
作为补充说明 - 你的功能变得有点笨拙。问题是你在函数中做了两件完全不同的事情 - 一件是生成随机的品质列表,另一件是从这些品质中生成描述。将第一个if
块中的内容移动到其自己的函数generateRandomDescription()
或其他内容中可能会更好,这些函数只会在第一个if
块中调用。
答案 1 :(得分:2)
当您检查height
是否大于或小于1.5或1.8时,如果等于其中一个值,则没有任何内容。将这些点的比较更改为>=
和<
,或>
和<=
。