Python 3.x 没有指定错误,但它不符合我的预期或要求
出于某种原因,下面的代码不会出现错误,但每次都会出现相同的“谜题”。注意,这些谜题将在代码运行后添加。有人能提出可能的原因和解决方案吗Python 3.x 没有指定错误,但它不符合我的预期或要求,python-3.x,Python 3.x,出于某种原因,下面的代码不会出现错误,但每次都会出现相同的“谜题”。注意,这些谜题将在代码运行后添加。有人能提出可能的原因和解决方案吗 import random correct = 0 not_again = [] puzzle = ["SUP","SAS","FUN","IS"] answer = ["SUP","SAS","FUN","IS"] life = 10 print("Welcome to the labrynth") print("You are an explorer") p
import random
correct = 0
not_again = []
puzzle = ["SUP","SAS","FUN","IS"]
answer = ["SUP","SAS","FUN","IS"]
life = 10
print("Welcome to the labrynth")
print("You are an explorer")
print("You have entered the maze in the hope of finding the lost treasure")
print("There will be a door blocking your entrance and preceedings at which a puzzle will be presented")
print("If you get the answer correct the door will open")
print("Then another 9 puzzles arrive at which point there will be the ultimate puzzle giving you the treasure")
print("If you get it wrong you lose one of ten lives")
def puzzles():
global correct
global not_again
global puzzle
global answer
global life
print("A stone door blocks your path")
print("An inscription is on it: a riddle")
select = random.randint(1,4)
select -= 1
select = int(select)
if select in not_again:
select = random.randint(1,4)
select -= 1
for select in range(len(puzzle)):
puz = puzzle[select]
print(puz)
doesit = input("Type your answer")
if doesit == answer[select]:
print("The door opens!")
correct += 1
not_again.append(select)
print(not_again)
if correct < 10:
puzzles()
elif doesit != answer[select]:
life -= 1
not_again.append(select)
if correct < 10:
puzzles()
puzzles()
不完全确定这是否是您的问题,但您似乎无意中覆盖了第一个for循环中的select变量 我理解random.randint1,4的方式应该在指定的范围内生成一个整数,用于确定将向用户提供什么谜题。该数字存储在变量select中。然而在循环中
for select in range(len(puzzle)):
puz = puzzle[select]
您将使用列表拼图的长度覆盖该变量,该长度是静态的,始终为3。试着消除这个for循环,看看它是否能产生您想要的结果
def puzzles():
global correct
global not_again
global puzzle
global answer
global life
print("A stone door blocks your path")
print("An inscription is on it: a riddle")
select = random.randint(1,4)
select -= 1
select = int(select)
if select in not_again:
select = random.randint(1,4)
select -= 1
puz = puzzle[select]
print(puz)
doesit = input("Type your answer")
if doesit == answer[select]:
print("The door opens!")
correct += 1
not_again.append(select)
print(not_again)
if correct < 10:
puzzles()
elif doesit != answer[select]:
life -= 1
not_again.append(select)
if correct < 10:
puzzles()
对社区-这是我的第一个答案,如果我违反了某些规定,请让我知道,我将予以纠正谢谢,似乎一切正常