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

2024-10-05 22:03:40 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我在为python做一个在线课程,有一个“猜猜电影”游戏的测试示例代码。然而,我试图按照几乎相同的逻辑自己写它,但似乎有一个错误,多个字母被解锁,而不是只有一个

例如: 罗布,轮到你了


你的信:o

  • o*l

正如你所看到的,虽然我之前没有输入,但最后一个字母“l”也被解锁,而不是显示只有字母“o”被解锁。这里的电影叫做“灵魂”。 在输入字母“S”时,它显示:

按1猜电影,或按2解锁另一个角色2 你的信:S

S o u l

电影完全解锁。如果你能在我的代码中找到错误,请给我一个解决方案。 我的代码:

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

     

Tags: toforinputifisnotmovietemp
2条回答

我运行了几次您的代码并对其进行了测试后,发现了问题:

unlock函数中,您犯了一个错误:

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 

 
            

相关问题 更多 >