没有指定错误,但它没有达到我的预期或想要

时间:2017-05-28 14:56:41

标签: 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")
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()

1 个答案:

答案 0 :(得分:0)

不完全确定这是否是您的问题,但您似乎无意中在第一个for循环中覆盖了select变量。

我理解random.randint(1,4)的方式应生成指定范围内的整数,以确定将向用户提供的拼图。该数字存储在变量select中。但是在循环中

for select in range(len(puzzle)):
    puz = puzzle[select]

你用列表puzzle的长度覆盖了那个变量,它是静态的,总是“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()

(对社区 - 这是我的第一个答案,如果我违反了某些内容,请告诉我,我会纠正)