簡體   English   中英

如何修復我在 Python 中編寫的“猜電影”游戲的邏輯

[英]How to fix the logic of "Guess the Movie" Game i have written in Python

所以,我正在為 python 做一個在線課程,並且有一個“猜電影”游戲的測試示例代碼。 然而,我試圖按照幾乎相同的邏輯自己編寫它,但似乎有一個錯誤,多個字母被解鎖而不是一個。

例如:ROB,輪到你了


你的信:o

  • o * l

正如你所看到的,而不是顯示只有字母“o”被解鎖,最后一個字母“l”也被解鎖,即使我之前沒有輸入它。這里的電影叫做“靈魂”。 輸入字母“S”后顯示:

按 1 猜測電影或按 2 解鎖另一個角色 2 你的字母:S

靈魂

電影已完全解鎖。如果你能找到我代碼中的錯誤,請給我一個解決方案。 我的代碼:

import random
Films=["Deadpool","Avengers Endgame","Drishyam","Hera Pheri","Munna Bhai MBBS","Justice League","The Dark Knight","Cars","Titanic","Haseena Man Jayegi","Uri Surgical Strike","Guardians of the Galaxy","Interstellar","Inception","The Great Gatsby","John Wick","Spiderman Homecoming","Bajirao Mastani","Nobody","Life of Pi","Krish","Golmaal","Housefull","Zindagi Na Milegi Dobara","3 idiots","Dangal","Badshah","The Shawshank Redemption","Frozen","Soul","Despicable Me","Minions","Crossroads"]

def create_question(Movie):    
    n=len(Movie)
    letters=list(Movie)
    temp=[]
    for i in range(n):
        if letters[i]== " ":
           temp.append(" ")
        else:
           temp.append("*")
    Q =" ".join(str(x) for x in temp)
    return Q

def is_present(letter,Movie):
    c=Movie.count(letter)
    if c==0:
        return False
    else:
        return True
    
def unlock(Q,picked_Movie,letter):
    ref=list(picked_Movie)
    Q_list=list(Q)
    temp=[]
    n=len(picked_Movie)
    for i in range(n):
        if ref[i]==" " or ref[i]==letter:
           temp.append(ref[i])
        else:
          if Q_list[i]=="*":
            temp.append("*")
          else:
              temp.append(ref[i])
    
    Q_new =" ".join(str(x) for x in temp)
    return Q_new         
            
            
            
    
    

def game():
    pA=input("Player 1 Name:")
    pB=input("Player 2 Name:")
    pp1=0
    pp2=0
    turn=0
    willing=True
    while willing:
        if turn%2==0:
            print(pA,",your turn")
            picked_Movie=random.choice(Films)
            Q=create_question(picked_Movie)
            print(Q)
            modified_Q=Q
            not_said=True 
            while not_said:
                letter=input("Your letter:")
                if(is_present(letter,picked_Movie)):
                    modified_Q = unlock(modified_Q,picked_Movie,letter)
                    print(modified_Q)
                    d=int(input("Press 1 to guess the movie or 2 to unlock another character"))
                    if d==1:
                        ans=input("Answer:")
                        if ans==picked_Movie:
                            print("Yay! Correct answer.")
                            pp1=pp1+1
                            print(pA,"'s Score=",pp1)
                            not_said=False
                        else:
                            print("Wrong Answer, Try again...")
                            
                            
                 
                else:
                    print(letter,'not found')
            c=int(input("press 1 to continue or 0 to exit:"))
            if c==0:
                print(pA,",Your Score is",pp1)
                print(pB,",Your Score is",pp2)
                print("Thank you for playing, have a nice day!!!")
                willing=False
                    
        else: 
            print(pB,",your turn")
            picked_Movie=random.choice(Films)
            Q=create_question(picked_Movie)
            print(Q)
            modified_Q=Q
            not_said=True 
            while not_said:
                letter=input("Your letter:")
                if(is_present(letter,picked_Movie)):
                    modified_Q = unlock(modified_Q,picked_Movie,letter)
                    print(modified_Q)
                    d=int(input("Press 1 to guess the movie or 2 to unlock another character:"))
                    if d==1:
                        ans=input("Answer:")
                        if ans==picked_Movie:
                            print("Yay! Correct answer.")
                            pp2=pp2+1
                            print(pB,"'s Score=",pp2)
                            not_said=False
                        else:
                            print("Wrong Answer, Try again...")
                else:
                    print(letter,'not found')
            c=int(input("press 1 to continue or 0 to exit:"))
            if c==0:
                print(pA,",Your Score is",pp1)
                print(pB,",Your Score is",pp2)
                print("Thank you for playing, have a nice day!!!")
                willing=False
             
        turn=turn+1
game()         

     

幾次運行你的代碼后,測試它,我發現了問題:

里面unlock function,你做錯了:

for i in range(n):
        if ref[i]==" " or ref[i]==letter:
           temp.append(ref[i])
        else:
          if Q_list[i]=="*":
            temp.append("*")
          else:
              temp.append(ref[i])

您只檢查了Q_list[i]是否有* 但是如果里面有" "怎么辦? 然后你會無緣無故地收到來自ref[i]的另一封信!

您唯一需要做的就是修改 if 語句:

for i in range(n):
        if ref[i]==" " or ref[i]==letter:
           temp.append(ref[i])
        else:
          if Q_list[i]=="*" or Q_list[i] == " ":        #  <-- FIX HERE
            temp.append("*")
          else:
              temp.append(ref[i])

編輯:

我看到在某些情況下我的代碼仍然無法正常工作,這就是為什么:如果電影名稱中有" " ,那么Q_list將比ref大,這會給我們帶來意想不到的結果。

您可以輕松修復它,刪除*之間的所有" " 到處都有:

" ".join(str(x) for x in temp)

這在您的代碼中(實際上是兩次),將其更改為:

"".join(str(x) for x in temp)

我剛剛更改了unlock()方法,如下所示(更改在注釋中提到)

def unlock(Q,picked_Movie,letter):
    ref=list(picked_Movie)
    Q_list=list(Q)
    temp=[]
    n=len(picked_Movie)
    for i in range(n):
        if ref[i]==" " or ref[i]==letter:
           temp.append(ref[i])
        else: 
            if Q_list[i]=="*" or Q_list[i] == " ":  #Added 1 more condition Q_list[i] == " "
              temp.append("*")
            else:
                temp.append(ref[i])
    Q_new ="".join(str(x) for x in temp)    #Replaced " " to ""
    return Q_new 

 
            

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM