在函数之间传递变量

问题描述:

我在很多地方都在这个网站上搜索过,但无法理解它,因为当我试图将它实现到我的代码中时,它不起作用。在函数之间传递变量

下面是完整的代码。

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循环和全局变量设置。

+0

请链接到没有意义或在你的代码没有工作的问题,我可以找到大量的资源,解释如何改变'global'变量在功能上。 –

+0

也许看到https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python –

这样做有两种方式:

  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() 
    

    唯一的问题正如Docs所说:

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

  2. 或者你可以用参数做到这一点,这是在函数(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)