在函数之间传递变量

时间:2016-05-16 15:56:35

标签: python-3.x

我在很多地方搜索过这个网站,但是无法理解它,因为当我尝试将它实现到我的代码中时,它没有用。

这是完整的代码。

from random import randint
from time import sleep

def keepscore():
    score = 0
    cscore = 0
return score, cscore


def gamestart():
    print("""Starting in:
  _____             
 |___ /             
   |_ \             
  ___) |  _   _   _ 
 |____/  (_) (_) (_)

                """)
sleep(1)
print("""
  ____              
 |___ \             
   __) |            
  / __/   _   _   _ 
 |_____| (_) (_) (_)

                    """)
sleep(1)
print("""
  _             
 / |            
 | |            
 | |  _   _   _ 
 |_| (_) (_) (_)

            """)
sleep(1)
play()

def play():
    score,cscore = keepscore()
    rps = ["Rock","Paper","Scissors"]
    cpu = rps[randint(0,2)]
    ask = input("Rock, Paper, Scissors? ")
    if ask == "Rock":
        if cpu == "Rock":
        sleep(0.5)
        print("\nThe computer chose Rock!")
        print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Paper":
        sleep(0.5)
        print("\nThe computer chose Paper!")
        print("You lose!")
        cscore += 1
        print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Scissors":
        sleep(0.5)
        print("\nThe computer chose Scissors!")
        print("You win!")
        score += 1
        print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif ask == "Paper":
    if cpu == "Rock":
        sleep(0.5)
        print("\nThe computer chose Rock!")
        print("You lose!")
        cscore += 1
        print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Paper":
        sleep(0.5)
        print("\nThe computer chose Paper!")
        print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Scissors":
        sleep(0.5)
        print("\nThe computer chose Scissors!")
        print("You win!")
        score += 1
        print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
elif ask == "Scissors":
    if cpu == "Rock":
        sleep(0.5)
        print("\nThe computer chose Rock!")
        print("You lose!")
        cscore += 1
        print("Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Paper":
        sleep(0.5)
        print("\nThe computer chose Paper!")
        print("You win!")
        score += 1
        print("Your score is: " + str(score) + ".  The computer's score is: " + str(cscore) + ".\n")
    elif cpu == "Scissors":
        sleep(0.5)
        print("\nThe computer chose Scissors!")
        print("It's a tie! Your score is: " + str(score) + ". The computer's score is: " + str(cscore) + ".\n")
else:
    print("That wasn't a valid option. Please try again.\n")
return score,cscore

def best():
    bestof = input("Best of: 1, 2, 3, 4, or 5? ")
    if bestof == "1":
        for i in range(1):
            gamestart()
    elif bestof == "2":
        for i in range(2):
            gamestart()
    elif bestof == "3":
        for i in range(3):
            gamestart()
    elif bestof == "4":
        for i in range(4):
            gamestart()
    elif bestof == "5":
        for i in range(5):
            gamestart()
    elif int(bestof) >= 5:
        sleep(0.5)
        print("...")
        sleep(1)
        print("Really?... Fine.\n")
        sleep(2)
        for i in range(int(bestof)):
            gamestart()
    else:
        print("Defaulting to 3...")
        for i in range(3):
            gamestart()

keepscore()
best()

我要做的是在循环的每次迭代后保持分数,但每次重置为0。 我尝试了多种方法,包括while循环和全局设置变量。

1 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点:

  1. 正如@Tadhg麦当劳 - 詹森所说,你可以通过global装饰,一个例子看起来像这样:

    var1 = 0
    def my_fun1():
        global var1 #the Global decoration
        var1 = int(input('Input a number: '))
    
    def my_fun2():
        if var1 % 2 == 0:
            print(var1, 'is an even number!')
    
    my_fun1()
    my_fun2()
    

    唯一的问题是文件说:

      

    全局声明中列出的名称不得定义为正式名称   参数或for循环控制目标,类定义,函数   定义或导入声明。

  2. 或者你可以用 Argument 来做,这是函数末尾括号之间的值(my_fun()<--那些)。一个例子如下:

    var1 = int(input('Input a number: '))
    
    def my_fun1(var2):
        if var2 % 2 == 0:
            print(var2, 'is an even number!')
    
    my_fun1(var1)