Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 不';t在第二次运行时存储输入的值_Python_List_Function_If Statement_Random - Fatal编程技术网

Python 不';t在第二次运行时存储输入的值

Python 不';t在第二次运行时存储输入的值,python,list,function,if-statement,random,Python,List,Function,If Statement,Random,我做了一个石头、布、剪刀的游戏。 正如我所希望的,它工作得很好,但当第二次运行时,它不会存储输入的值,我如何修复它 请参阅下面的while循环,如果用户输入y,程序将再次运行 import random tools =["Rock","Scissors","Paper"] r = "".join(tools[0]) s = "".join(tools[1]) p = "".j

我做了一个石头、布、剪刀的游戏。 正如我所希望的,它工作得很好,但当第二次运行时,它不会存储输入的值,我如何修复它

请参阅下面的
while
循环,如果用户输入
y
,程序将再次运行

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!")

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

computer = computer_predicion()
human = my_predicion()
在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!")
这是您的工作代码,我在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!")

哇,虽然修理起来没那么容易。非常感谢。但有一个问题。当您运行此代码时,它会要求您输入两次值,所以若要修复此错误,您需要在用户选择继续后请求新值。@ivosorkins您已修复该问题吗?或者你还需要帮助吗?是的,我必须在函数中加入变量。