2014-03-07 39 views
0

我的代码是指当比赛赢来显示一个双赢的消息,但即使当其完成游戏会显示一个重试的消息。Python的 - 不打印胜利消息

我的代码看起来像

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* 

solved.txt是

acquired 
almanac 
insult 
joke 
hymn 
gazelle 
amazon 
eyebrows 
affix 
vellum 

clues.txt是

A = # 
M = * 
N = % 
+1

检查条件的压痕/位置。你可能想在循环中检查i ==(x-1),并且如果检查拼图是否解决了(或者其他方法......),还有其他方法。不管怎样,它看起来像,即使你解决了谜题,它不断循环......最终的失败。一旦你解决了这个难题,你就需要休息。 – TheOneWhoPrograms

+0

@TheOneWhoPrograms林新本:/你可以编辑我的代码,并张贴?那将是非常感谢 – user3392493

+0

可悲的是,我没有安装的Python解释器,我写的任何代码将被取消选中,并可能在整个过程中有一些小错误。如果没有人发表我所做的工作的时间的答案,我会回家和安装python IDE,使这个为你工作。 (如果我这样做,我会希望你的输入文件,所以你可能想要在你的开放文章中粘贴一些示例输入文件。 – TheOneWhoPrograms

回答

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

这个问题非常感谢! !我会在学校测试这个,你刚给我一个A *:D – user3392493

+0

我刚刚测试过它,它的工作很完美,只需要输入时间,就可以编辑它,所以当用户获胜时,它不会要求玩再次,我非常感谢你的帮助 – user3392493