Python - 不打印赢取消息

时间:2014-03-07 12:04:36

标签: python python-3.x

我的代码是为了在赢得游戏时显示赢取消息,但即使完成游戏也会再次显示尝试消息。

我的代码看起来像

import time

print("If A is represented by the symbol #, M by * and N by %, then the word\nMANNA would be represented by the symbols *#%%#")

#Loads files

in_file = open("words.txt", 'rt')
words_loaded = in_file.read()
print(words_loaded)
in_file.close()

print("Here are your clues!")
time.sleep(0.5)

in_file = open("clues.txt", 'rt')
clues_loaded = in_file.read()
print(clues_loaded)
in_file.close()

in_file = open("solved.txt", 'rt')
solved = in_file.read()
in_file.close()

#Menu

def menu():

        play_game = print("1. Play the game")
        instruc = print("2. Instructions")
        question = input("Enter choice")

        if question == "2":
                print("You will given a list of coded words, you have to replace the symbols to letters to get the word")
                print("\n")
                menu()
        else:
                play()

#How many turns user would like

def play():

        global words_loaded

        x = int(input("How many guesses would you like?\n(You'll need atleast 23, only input numbers) \n"))
        for i in range(x):



#symbol replacing

                sym = input("Which symbol do you want to replace? ")
                cha = input("Which character do you want to replace with it? ")

#shows the symbol changes

                words_loaded = words_loaded.replace(sym, cha)
                print("Your Puzzle now looks like... \n")
                print(words_loaded)

#Messages for user if they win or lose

                if words_loaded == solved:
                    print("PUZZLE SOLVED")
        if i ==(x-1):
                print("Better luck next time")
                ask = input("would you like to play again?")

                if ask == "yes":
                        play()
                else:
                        print("bye")
                     Asks if user would like to play again
                Tryagain = input("would you like to play?")

                if Tryagain == "no":
                       print("Thanks for playing")
                else:
                       menu()

menu()

它可能需要在底部附近修复,只是认为id包含整个代码。

的内容

words.txt是

#+/084&"
#3*#%#+
8%203:
,1$&
!-*%
.#7&33&
#*#71%
&-&641'2
#))85
9&330*

solve.txt是

acquired
almanac
insult
joke
hymn
gazelle
amazon
eyebrows
affix
vellum

clues.txt是

A = #
M = *
N = %

1 个答案:

答案 0 :(得分:0)

for循环附近有缩进错误。修复了解决问题的方法,但我不明白为什么你再次要求再次播放,所以我将其改为一个if-elif-else语句。
此外,我将文件的开放时间更改为with,因为它更易于阅读,您无需手动close

import time

def menu():

        play_game = print("1. Play the game")
        instruc = print("2. Instructions")
        question = input("Enter choice")

        if question == "2":
                print("You will given a list of coded words, you have to replace the symbols to letters to get the word")
                print("\n")
                menu()
        else:
                play()

#How many turns user would like

def play():

        global words_loaded

        x = int(input("How many guesses would you like?\n(You'll need atleast 23, only input numbers) \n"))
        for i in range(x):



#symbol replacing

                sym = input("Which symbol do you want to replace? ")
                cha = input("Which character do you want to replace with it? ")

#shows the symbol changes

                words_loaded = words_loaded.replace(sym, cha)
                print("Your Puzzle now looks like... \n")
                print(words_loaded)

#Messages for user if they win or lose

                if words_loaded == solved:
                    print("PUZZLE SOLVED")
                    break


        if i ==(x-1):
               print("Better luck next time")
#        ask = input("would you like to play again?")

#Asks if user would like to play again
        Tryagain = input("would you like to play?")

        if Tryagain == "yes":
                play()

        elif Tryagain == "no":
               print("Thanks for playing")

        else:
               menu()

print("If A is represented by the symbol #, M by * and N by %, then the word\nMANNA would be represented by the symbols *#%%#")

#Loads files

with open("words.txt", 'rt') as in_file:
        words_loaded = in_file.read()
        print(words_loaded)

print("Here are your clues!")
time.sleep(0.5)

with open("clues.txt", 'rt') as in_file:
            clues_loaded = in_file.read()
            print(clues_loaded)

with open("solved.txt", 'rt') as in_file:
            solved = in_file.read()


#Menu

menu()