2014-03-07 46 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