添加到无效的变量?

时间:2017-12-21 02:50:47

标签: python

在变量中添加数字不起作用? (这又是石头剪刀程序。)

    import random
hscore = 0
cscore = 0
tries = 0


#computer choice
rock = ("rock")
paper = ("paper")
scissors= ("scissors")
rps = (rock, paper, scissors)

#human wins
def humanfunction():
    hscore +=1
    if choice == "rock":
        if cchoice == scissors:
            hscore +=1
            print("Human wins this round.")
    if choice == "scissors":
        if cchoice == paper:
            hscore +=1
            print("Human wins this round.")
    if choice == "paper":
        if cchoice == rock:
            hscore +=1
            print("Human wins this round.")
def computerwin():

#computer wins
    cscore +=1
    if cchoice == "rock":
        if choice == scissors:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "scissors":
        if choice == paper:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "paper":
        if choice == rock:
            cscore +=1
            print("Computer wins this round.")
def tie():

    if cchoice == choice:          
        print("It's a tie!")

#choosing
while 0 == 0:

    choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
    tries +=1
    cchoice = random.choice(rps)
    humanfunction()
    computerwin()
    tie()
    print(hscore, cscore)
    print("Human choice: ",choice)
    print("Computer choice: ",cchoice)
    print("Finished game number", tries)
    if tries == 10:
        print("limit reached")
        break

当我运行代码时,它会显示此信息(事先忽略错误):

enter image description here

人类选择了纸,计算机选择了摇滚,这意味着人类击败了计算机 - 但是人类的得分并没有增加下一场比赛。由于某种原因,函数一直在严重破坏我的代码。

hscore在函数中再次存在,因为没有它,输出将说明事先引用的局部变量。 (这是一个课堂作业,我们不能使用我们还没有完成的事情。)所以添加全局也无济于事。选项在稍后的代码中定义。我只是没有包含它,因为我只想显示添加变量的部分。

2 个答案:

答案 0 :(得分:0)

对于每个函数调用,

变量hscore被重新初始化为0,这使得它全局化将为您带来所需的结果,同样适用于cscore。

使hscore和cscore全局如下:

import random
hscore = 0
cscore = 0
tries = 0


#computer choice
rock = ("rock")
paper = ("paper")
scissors= ("scissors")
rps = (rock, paper, scissors)

#human wins
def humanfunction():
    global hscore
    hscore +=1
    if choice == "rock":
        if cchoice == scissors:
            hscore +=1
            print("Human wins this round.")
    if choice == "scissors":
        if cchoice == paper:
            hscore +=1
            print("Human wins this round.")
    if choice == "paper":
        if cchoice == rock:
            hscore +=1
            print("Human wins this round.")

def computerwin():
#computer wins
    global cscore
    cscore +=1
    if cchoice == "rock":
        if choice == scissors:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "scissors":
        if choice == paper:
            cscore +=1
            print("Computer wins this round.")
    if cchoice == "paper":
        if choice == rock:
            cscore +=1
            print("Computer wins this round.")
def tie():

    if cchoice == choice:          
        print("It's a tie!")

#choosing
while 0 == 0:
    choice = input("\nWhat do you choose? <rock, paper, scissors>: ")
    tries +=1
    cchoice = random.choice(rps)
    humanfunction()
    computerwin()
    tie()
    print(hscore, cscore)
    print("Human choice: ",choice)
    print("Computer choice: ",cchoice)
    print("Finished game number", tries)
    if tries == 10:
        print("limit reached")
        break

<强>输出:

What do you choose? <rock, paper, scissors>: 0
(1, 1)
('Human choice: ', 0)
('Computer choice: ', 'rock')
('Finished game number', 1)

What do you choose? <rock, paper, scissors>: 1
(2, 2)
('Human choice: ', 1)
('Computer choice: ', 'scissors')
('Finished game number', 2)

What do you choose? <rock, paper, scissors>: 0
(3, 3)
('Human choice: ', 0)
('Computer choice: ', 'scissors')
('Finished game number', 3)

What do you choose? <rock, paper, scissors>: 0
(4, 4)
('Human choice: ', 0)
('Computer choice: ', 'paper')
('Finished game number', 4)

What do you choose? <rock, paper, scissors>: 2
(5, 5)
('Human choice: ', 2)
('Computer choice: ', 'paper')
('Finished game number', 5)

What do you choose? <rock, paper, scissors>: 2
(6, 6)
('Human choice: ', 2)
('Computer choice: ', 'rock')
('Finished game number', 6)

答案 1 :(得分:0)

您的代码当前和潜在的问题很多,因此这个问题的最佳答案可能就是解决这个问题的一般方法,这可能会让您深入了解Python程序的结构。< / p>

你的函数的主循环很好,但它期望函数改变程序的一般状态。这不是通常应该做的功能(除非在极少数情况下)。相反,函数应该接受参数并返回结果。在这种情况下,你需要一个带有两个参数的函数 - 玩家的选择和计算机的选择 - 并返回谁获胜。

考虑以下伪代码:

function judge_game: 
    arguments: player choice, computer choice
    return: 'tie' or 'player' or 'computer'

initialize variables for tries and scores

while tries are sufficiently small
    get player choice
    get computer choice

    if judge_game returns 'player' then increment player score
    if judge_game returns 'computer' then increment computer score

    # carry on with remaining logic

如果你重新考虑这个骨架的问题,你可能会发现你的大部分变量和范围问题已经消失了。