我编写了一个小游戏,给了我一些绘画建议。我还实现了一个随机难度,对于中等难度和中等难度它都可以正常工作,但是当选择了困难难度时,它会给我一个错误,告诉我该变量未定义。当我通过vscode调试器运行它时,它的工作原理就很好。
# Importing random module
import random
# User input
difficulty = int(input("Please choose your difficultie: 1 = Easy, 2 = Medium, 3 = Hard, 4 = Random difficulty"))
# Adding the random difficulty option
if difficulty == 4:
difficulty = random.randint(0,4)
if difficulty == 1:
difficult = "Easy"
elif difficulty == 2:
difficult = "Medium"
elif difficulty == 3:
difficult = "Hard"
print("The random difficulty that has been chosen is ", difficult)
# Setting up variables
# Heroes
a = "Spiderman, "
b = "Superman, "
c = "Batman, "
# Villains
aa = "fighting the Green Goblin, "
bb = "fighting Doomsday, "
cc = "fighting The Joker, "
# Circumstances
aaa = "on top of a Skyscraper"
bbb = "in the Streets of Metropolis"
ccc = "inside The Batcave"
# Powers
aaaa = "with weather control powers, "
bbbb = "with sound control powers, "
cccc = "without superpowers, "
# Villain Powers
aaaaa = "who is invisible, "
bbbbb = "who has a big laser-beam, "
ccccc = "who has a remotely controlled tornado, "
# Choosing the random variables for easy difficulty
if difficulty == 1:
final_hero = random.choice([a, b, c])
final_villain = random.choice([aa, bb, cc])
final_circumstance = random.choice([aaa, bbb, ccc])
final_string_easy = final_hero + final_villain + final_circumstance
print(final_string_easy)
# Choosing the random variables for medium difficulty
if difficulty == 2:
final_hero = random.choice([a, b, c])
final_villain = random.choice([aa, bb, cc])
final_circumstance = random.choice([aaa, bbb, ccc])
final_power = random.choice([aaaa, bbbb, cccc])
final_string_medium = final_hero + final_power + final_villain + final_circumstance
print(final_string_medium)
# Choosing the random variables for hard difficulty
if difficulty == 3:
final_hero = random.choice([a, b, c])
final_villain = random.choice([aa, bb, cc])
final_circumstance = random.choice([aaa, bbb, ccc])
final_power = random.choice([aaaa, bbbb, cccc])
final_villain_power = random.choice([aaaaa, bbbbb, ccccc])
final_string_hard = final_hero + final_power + final_villain + final_villain_power + final_circumstance
print(final_string_hard)
我们将不胜感激。
答案 0 :(得分:1)
我将变量difficulty
重命名为football
,将difficult
重命名为bananas
,这样您就可以更容易地发现错误
import random
football = int(input("Please choose your difficulty: 1 = Easy, 2 = Medium, 3 = Hard, 4 = Random difficulty"))
if football == 4:
football = random.randint(0,4)
if football == 1:
bananas = "Easy"
elif football == 2:
bananas = "Medium"
elif football == 3:
bananas = "Hard"
print("The random difficulty that has been chosen is ", bananas)
如果football
既不是1,2也不是3,即既不是0也不是4,则bananas
未被定义。
因此是您的错误。
我怀疑您想更改为
football = random.randint(1,3)
答案 1 :(得分:1)
问题出在这里,您在提供1, 2, 3
的选项,但您正在使用randint
从0, 1, 2, 3, 4
中选择,但不会啮合
if difficulty == 4:
difficulty = random.randint(0,4)
if difficulty == 1:
difficult = "Easy"
elif difficulty == 2:
difficult = "Medium"
elif difficulty == 3:
difficult = "Hard"
总体而言,我认为在这种情况下使用dictionaries
可以消除您面临的所有重复问题。您可以设置两个词典来处理我们最后一句话的parts
。我们可以使用列表中的键和值。接下来,我们可以设置options
,它是keys
中的parts
。然后,我们可以创建另一个字典mode
,该字典将通过切片parts
告诉我们将使用哪些键从option
中进行选择。
from random import randint, choice
parts = {
'Heroes': ['Spiderman, ', 'Superman, ', 'Batman, '],
'Villians': [
'fighting the Green Goblin, ', 'fighting Doomsday, ', 'fighting the Joker, '
],
'Circumstances': [
'on top of a Skyscraper ', 'in the Streets of Metropolis ', 'inside The Batcave '
],
'Powers': [
'with weather control powers, ', 'with sound control powers, ', 'without superpowers, ' ],
'Villian Powers': [
'who is invisible.', 'who has a big laser-beam. ',
'who has a remotely controlled tornado. '
]
}
options = list(parts.keys())
mode = {1: options[:-2], 2: options[:-1], 3: options}
diff = int(input('Choose difficulty 1 - 3 (4 == random): '))
if diff == 4:
diff = randint(1, 3)
final_s = ''
for i in mode[diff]:
final_s += choice(parts[i])
print(final_s)
Choose difficulty 1 - 3 (4 == random): 4 Spiderman, fighting Doomsday, inside The Batcave Choose difficulty 1 - 3 (4 == random): 3 Superman, fighting the Joker, inside The Batcave with weather control powers, who is invisible.
答案 2 :(得分:0)
您正在生成一个从0到3的随机数。当数字为0时,变量 difficult 仍未定义,因为所有if子句都不匹配0。因此,在打印时,变量困难仍然没有价值。