2016-05-16 164 views
-1

我在很多地方都在這個網站上搜索過,但無法理解它,因爲當我試圖將它實現到我的代碼中時,它不起作用。在函數之間傳遞變量

下面是完整的代碼。

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 –

回答

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

    唯一的問題正如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)