【问题标题】:Doesn't store entered value when runs second time第二次运行时不存储输入的值
【发布时间】:2021-05-29 17:16:25
【问题描述】:

我做了一个石头剪刀布游戏。 它工作正常,正如我想要的那样,但是在第二次运行时它不会存储输入的值,我该如何解决这个问题。

如果用户输入y,请参见下面的while循环。

import random

tools =["Rock","Scissors","Paper"]
r = "".join(tools[0])
s = "".join(tools[1])
p = "".join(tools[-1])

def computer_predicion():
    computer_pred = random.choice(tools)
    return computer_pred

computer = computer_predicion()

def my_predicion():
    my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
    if my_predicion == "R" or my_predicion == "r":
        my_predicion = r
        return my_predicion
    elif my_predicion == "S" or my_predicion == "s":
        my_predicion = s
        return my_predicion
    elif my_predicion == "P" or my_predicion == "p":
        my_predicion = p
        return my_predicion
    else:
        print("Debils ir?")

human=my_predicion()

def game():
    message_win = ("You won!")
    message_lose = ("You lost!")
    message = "Computer:%s\nUser:%s"%(computer, human)
    if computer == r and human == r :
        print(message+"\nIt's draw")
    elif computer == p and human == p:
        print(message + "\nIt's draw")
    elif computer == s and human == s:
        print(message + "\nIt's draw")
    elif computer == r and human == p:
        print(message+'\n'+message_win)
    elif computer == p and human == r:
        print(message+'\n'+message_lose)
    elif computer == r and human == s:
        print(message+'\n'+message_lose)
    elif computer == s and human == r:
        print(message+'\n'+message_win)
    elif computer == p and human == s:
        print(message+'\n'+message_win)
    elif computer == s and human == p:
        print(message+'\n'+message_lose)
    else:
        pass

c = True
while c:      //Here code runs second time if user inputs Y or y.
    game()
    h = input("Continue?(Y/N):")
    if h == "Y" or h == "y":
        my_predicion()
        computer_predicion()
        pass
    elif h == "N" or h == "n":
        c = False
    else:
        print("Wrong symbol!")

【问题讨论】:

    标签: python list function if-statement random


    【解决方案1】:

    该值没有存储在第二个循环中,因为

    computer = computer_predicion()
    human = my_predicion()
    

    在while循环之外显示

    这是你的工作代码,我在 while 循环中迁移了两个变量赋值

    import random
    
    tools =["Rock","Scissors","Paper"]
    r="".join(tools[0])
    s="".join(tools[1])
    p="".join(tools[-1])
    def computer_predicion():
        computer_pred = random.choice(tools)
        return computer_pred
    
    def my_predicion():
        my_predicion = input("Choose (R)Rock,(S)Scissors,(P)Paper:")
        if my_predicion=="R" or my_predicion =="r":
           my_predicion = r
           return my_predicion
        elif my_predicion=="S" or my_predicion =="s":
           my_predicion = s
           return my_predicion
        elif my_predicion=="P" or my_predicion =="p":
           my_predicion = p
           return my_predicion
        else:
             print("Debils ir?")
    
    def game():
        message_win = ("You won!")
        message_lose = ("You lost!")
        message = "Computer:%s\nUser:%s"%(computer,human)
        if computer ==r and human==r :
           print(message+"\nIt's draw")
        elif computer == p and human == p:
           print(message + "\nIt's draw")
        elif computer == s and human == s:
           print(message + "\nIt's draw")
        elif computer == r and human==p:
           print(message+'\n'+message_win)
        elif computer == p and human==r:
           print(message+'\n'+message_lose)
        elif computer == r and human==s:
           print(message+'\n'+message_lose)
        elif computer == s and human==r:
           print(message+'\n'+message_win)
        elif computer == p and human == s:
           print(message+'\n'+message_win)
        elif computer == s and human==p:
           print(message+'\n'+message_lose)
        else:
            pass
    
    c=True
    while c :      #Here code runs second time if user inputs Y or y.
      computer = computer_predicion()
      human = my_predicion()
      game()
      h = input("Continue?(Y/N):")
      if h=="Y" or h=="y":
         pass
      elif h=="N" or h=="n":
          c=False
      else:
           print("Wrong symbol!")
    

    【讨论】:

    • 哇,尽管 0_0 修复它从未如此简单。谢谢!
    • 但是有问题。当您运行此代码时,它会要求输入两次值,因此要修复该错误,您需要在用户选择继续后要求输入新值。
    • @IvoSorokins 你解决了这个问题了吗?还是您还需要帮助?
    • 是的,我必须将变量放入函数中。
    猜你喜欢
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多