简体   繁体   中英

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. 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


Your letter:o

  • 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'. and upon entering the letter 'S' it shows:

Press 1 to guess the movie or 2 to unlock another character 2 Your letter: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:

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 * . But what if it has " " in it? Then you will get another letter from ref[i] for no reason!

The only thing you need to do is to modify the if statement:

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.

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

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 

 
            

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM