簡體   English   中英

如何將我在另一個 function 中定義的值調用到另一個 function?

[英]How do I call the values that I have defined in another function to the other function?

我遇到的問題是,當我嘗試將它傳遞給另一個 function 時,它說缺少 3 個必需的 arguments,ZDBC11CAA5BDA99F77E6FB4DABD_word882E7DZ 我想傳遞的比較是猜測字,所以我可以使用它來比較隨機字

def InputData():
   guess_word = input("Guess a word")
   jug_words = GetJugWords()
   random_words = GetRandomLetters()

def Score(jug_words, random_words, guess_word):
    score = 0

    while True:
        print("Your phrase is: ", random_words)
        guess_word = input("Guess a word: ")
        score +=score
        if len(guess_word) == 2 or len(guess_word) ==3 :
            score += 1
        elif len(guess_word) == 4:
            score += 2
        elif len(guess_word) == 5:
            score += 3
        elif len(guess_word) == 6 or len(guess_word) == 7:
            score += 5
        elif len(guess_word) == 8:
            score += 8

        if guess_word not in jug_words and random_words:
            print("Game Over!")
            break
        print("Your score is now", str(score))
    print(f"your total score: {score}")

我建議您從InputData() function 返回值。 您正在使用的當前變量在 function 的 scope 之外不存在。

您可以作為元組返回,例如

def InputData():
    ...    
    return  guess_word, jug_words, random_words

然后在調用function時直接解包變量即可:

guess_word, jug_words, random_words = InputData()
Score(jug_words, random_words, guess_word)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM