Python-不打印win消息

Python-不打印win消息,python,python-3.x,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 =

我的代码意味着在游戏获胜时显示获胜消息,但即使游戏完成,也会显示重试消息

我的代码看起来像

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*
已解决的.txt是

acquired
almanac
insult
joke
hymn
gazelle
amazon
eyebrows
affix
vellum
clues.txt是

A = #
M = *
N = %

循环的
附近有一个缩进错误。解决了这个问题,但我不明白你为什么要求再次播放两次,所以我将其改为一个
if elif else
语句。
另外,我用
s将您的文件打开更改为
,因为它更易于阅读,而且您不需要手动关闭它们

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

检查您的缩进/位置条件。你可能想检查循环中的i==(x-1),如果检查谜题是否解决(或者反过来…),则使用else。无论如何,看起来,即使你解决了谜题,它也会继续循环。。。最终它失败了。一旦你解决了这个难题,你就需要破解。@TheNewHopRograms我是新来的:/你能帮我编辑代码并发布吗?很遗憾,我没有安装python解释器,我编写的任何代码都会被取消检查,并且可能会有一些小错误。如果在我完成工作时还没有人给出答案,我将回家安装python IDE并为您完成这项工作。(如果我这样做,我会想要你的输入文件,所以你可能想在你的开篇文章中粘贴一些示例输入文件。@TheNewHoprograms谢谢你,我在问题中添加了文件的内容非常感谢!!我会在学校测试这个,你刚刚给我一个A*:我刚刚测试了它,它工作得很好,只需要导入时间,就可以了na编辑它,这样当用户赢了,它就不会要求再次播放,我非常感谢你的帮助