简体   繁体   English

如何修复我在 Python 中编写的“猜电影”游戏的逻辑

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

So, I was doing an online course for python, and there was a test sample code for the "Guess The Movie", game.所以,我正在为 python 做一个在线课程,并且有一个“猜电影”游戏的测试示例代码。 However, I tried to write it myself following almost the same logic but there seems to be an error where multiple letters are being unlocked rather than only one.然而,我试图按照几乎相同的逻辑自己编写它,但似乎有一个错误,多个字母被解锁而不是一个。

for Example: ROB, your turn例如:ROB,轮到你了


Your letter:o你的信:o

  • o * l o * l

As you can see instead of showing that only the letter 'o' is unlocked, the last letter 'l' is also unlocked even though i have not entered it previously.the movie here is called 'Soul'.正如你所看到的,而不是显示只有字母“o”被解锁,最后一个字母“l”也被解锁,即使我之前没有输入它。这里的电影叫做“灵魂”。 and upon entering the letter 'S' it shows:输入字母“S”后显示:

Press 1 to guess the movie or 2 to unlock another character 2 Your letter:S按 1 猜测电影或按 2 解锁另一个角色 2 你的字母:S

S oul灵魂

The movie is completely unlocked.If you can find the mistake in my code, please give me a solution.电影已完全解锁。如果你能找到我代码中的错误,请给我一个解决方案。 My Code:我的代码:

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

     

After a few times I run your code, tested it, I found the problem:几次运行你的代码后,测试它,我发现了问题:

Inside unlock function, you did a mistake:里面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])

You only checked if the Q_list[i] has * .您只检查了Q_list[i]是否有* But what if it has " " in it?但是如果里面有" "怎么办? Then you will get another letter from ref[i] for no reason!然后你会无缘无故地收到来自ref[i]的另一封信!

The only thing you need to do is to modify the if statement:您唯一需要做的就是修改 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])

EDIT:编辑:

I saw that in some cases my code still doesn't work, and this is why: If the movie name had " " in it, then the Q_list will be bigger then ref which will give us unexpected results.我看到在某些情况下我的代码仍然无法正常工作,这就是为什么:如果电影名称中有" " ,那么Q_list将比ref大,这会给我们带来意想不到的结果。

You can fix it easily, removing all the " " between the * .您可以轻松修复它,删除*之间的所有" " Everywhere you have:到处都有:

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

this in your code (twice actually), changes it to:这在您的代码中(实际上是两次),将其更改为:

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

I just changed unlock() method as below (changes are mentioned as comment) :我刚刚更改了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