2017-05-28 44 views
-2

由于某些原因,下面的代码不会带来任何错误,但每次都会产生相同的“拼图”(注意,一旦代码运行后,拼图将在稍后添加)任何人都可以提出可能的原因和解决方法没有指定错误,但它没有做我期望或想要的

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() 

回答

0

不能完全肯定,如果这是你的问题,但你似乎是无意中覆盖在第一个for循环select变量。

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

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

你覆盖该变量与列表puzzle的长度,这是静态的,始终为“3”。尝试消除该循环,看看是否会产生你想要的结果。

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() 

(向社会 - 这是我的第一个答案,如果我违反了什么让我知道,我会纠正)

+0

谢谢它似乎是为了 – Seb

相关问题