Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.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,函数不是随机调用的_Python - Fatal编程技术网

Python,函数不是随机调用的

Python,函数不是随机调用的,python,Python,我已经完成了剪刀或布程序的编写。我有一个函数ai(),它决定计算机端的随机性。这个函数还打印我每次循环结束时调用的计算机的决定,然而,有时它只是不执行。你能帮我找出原因吗?任何改进建议都很受欢迎 这是我的职责 def ai(): number = random.randint(1,4) if number == 1: global aiinput aiinput = ("rock") print(("T

我已经完成了剪刀或布程序的编写。我有一个函数ai(),它决定计算机端的随机性。这个函数还打印我每次循环结束时调用的计算机的决定,然而,有时它只是不执行。你能帮我找出原因吗?任何改进建议都很受欢迎

这是我的职责

def ai():
    number = random.randint(1,4)
    if number == 1:
        global aiinput
        aiinput = ("rock")
        print(("The choice of computer is: "),aiinput)
    elif number == 2:
        aiinput = ("paper")
        print(("The choice of computer is: "),aiinput)
    elif number == 3:
        aiinput = ("scissors")
        print(("The choice of computer is: "),aiinput)

这是我代码的其余部分


import random

def ai():
    number = random.randint(1,4)
    if number == 1:
        global aiinput
        aiinput = ("rock")
        print(("The choice of computer is: "),aiinput)
    elif number == 2:
        aiinput = ("paper")
        print(("The choice of computer is: "),aiinput)
    elif number == 3:
        aiinput = ("scissors")
        print(("The choice of computer is: "),aiinput)

def draw():
    print ("Draw")
def loser():
    print ("Loser!")
def winner():
    print ("You won!")

print("Welcome to the game to quit prompt q")
while True:
    usrinp = input("rock, scissors or paper? you can also prompt r,s or p: ")
    ai()
    if usrinp == ("r") and aiinput == ("rock"):
        draw()
    elif usrinp == ("r") and aiinput == ("paper"):
        loser()
    elif usrinp == ("r") and aiinput == ("scissors"):
            winner()


    if usrinp == ("p") and aiinput == ("rock"):
        winner()
    elif usrinp == ("p") and aiinput == ("paper"):
        draw()
    elif usrinp == ("p") and aiinput == ("scissors"):
        loser()

    if usrinp == ("s") and aiinput == ("rock"):
        loser()
    elif usrinp == ("s") and aiinput == ("paper"):
        winner()
    elif usrinp == ("s") and aiinput == ("scissors"):
        draw()
    if usrinp == ("q"):
        quit()
在这里,您可以看到调试

ai
功能中,仅在
if
之后设置
全局aiinput
。在国际单项体育联合会之前做这件事。您还需要使用
randint(1,3)


您生成值1-4,并且只检查1-3,因此如果其值为4,则不会执行任何操作 以下是一个例子:

Return a number between 3 and 9 (both included):

import random

print(random.randint(3, 9))

原因有二: 首先,正如@Rolandas Ulevicius所说,您的随机数函数可以生成if语句无法处理的数字;看

其次,在函数
ai()
中,仅当ai选择1(即rock)时,才定义全局变量
aiinput
。更好的方法是在函数内部定义一个局部变量,然后返回值。在您的情况下,实现看起来有点像:

def ai():
    number = random.randint(1,3)
    if number == 1:
        print("The choice of computer is: rock")
        return "rock"
    elif number == 2:
        print("The choice of computer is: paper")
        return "paper"
    elif number == 3:
        print("The choice of computer is: scissor")
        return "scissors"
然后在while循环中,将
ai()
更改为
aiinput=ai()

有更好的方法来编写代码,但由于您似乎是一个相当初级的人,除非您特别需要,否则我不会将其包括在内


编辑:有关构造
ai()

的更好方法,请参见@Guimoute的答案。我相信它唯一不打印任何内容的时间是在
random.randint(1,4)
返回值4时。请添加
elif
案例以检查是否为4,或者最好添加
else
子句,因为您只允许它为4。感谢您指出。哦,我以为是4,但不包括4本身。我现在改为3,我想它可以工作了谢谢你^^如果你的问题解决了,请接受解决它的答案:)谢谢你的反馈
def ai():
    number = random.randint(1,3)
    if number == 1:
        print("The choice of computer is: rock")
        return "rock"
    elif number == 2:
        print("The choice of computer is: paper")
        return "paper"
    elif number == 3:
        print("The choice of computer is: scissor")
        return "scissors"