2017-03-31 64 views
0

所以基本上我試圖創建一個hang子手遊戲,而且現在我只是試圖將一個變量從一個函數傳遞到另一個函數,儘管我很困惑如何去做,下面是我的代碼:如何從包含多個變量的函數中只調用一個變量?

import random 

print("Welcome to Hangman!") 

def wordSelection(): 
    words = ["test", "random"] 
    wordChoice = random.randint(0, 1) 
    selectedWord = words[wordChoice] 
    print(selectedWord) 
    return selectedWord 

def guessWord(): 
    selectedWord = wordSelection() 
    print(selectedWord) 


wordSelection() 
guessWord() 

我的問題基本上是,我怎麼能夠從wordSelection函數調用變量'selectedWord',並把它傳遞到guessWord()功能?

我試圖在底部,因爲你可以看到,但我只是再次調用整個函數,而不是隻專門得到selectedWord而不是整個函數。

我該怎麼做才能從WordSelection()獲得'selectedWord'而不是僅僅獲得整個功能 - 這可能嗎?

+0

您可以像使用'selectedWord'那樣從函數返回值,並將它們存儲在變量中。您可以在調用它們時將這些變量作爲參數傳遞給其他函數。這可能是做你想做的一種方式,取決於你的邏輯,它可能工作與否。 – nbro

+1

僅供參考,你可以調用'random.choice(words)'。 – zipa

回答

0

你對變量是如何工作有誤解,特別是在Python中。

在Python中,變量只是一個標籤,指的是存儲在某處的值。例如:

x = 3 
# Now you can refer to the value `3` by the name x 
y = x 
# Now `y` refers to the *same* `3` that is in memory - it's not a copy! 
print(id(x)) 
print(id(y)) 
# Will print the same ID, but it will probably be different on your 
# system and mine 

當你從一個函數返回一個值,你不返回的名字 - 你有效,回訪的價值生活。如果你想保住你的價值,你將不得不給它一個名字:

def fun(): 
    result = 42 
    print(id(result)) 
    return result 

fun() # Discards the return value 
print(fun()) # prints the return value 
print(id(fun)) # prints where the return value lives 
value = fun() # actually stores the return value by giving it a name 
print(id(value)) # now you can use that value later! 
print(value + 3) # you can use it more times 
代碼

所以,你要調用一個函數,給你一個隨機單詞,你可以這樣做:

def word_selection(): 
    # Note I used snake case - see 
    # https://www.python.org/dev/peps/pep-0008/#function-names 
    # for more info 
    words = ['test', 'random', 'python', 'awesome', 'something', 'completely', 'different'] 
    return random.choice(words) 

爲了用這個值做任何事情,你需要存儲它。無論您何時調用該功能,它都會返回。

print(word_selection) # Doesn't call the function 
print(word_selection()) # Does call the function, then prints the result 

所以你可以要求你的guess_word功能的參數,並傳遞它:

def guess_word(word): 
    # do whatever you need to do here 
    print('Time to guess:', word) # don't really give them the answer, though 

guess_word(word_selection()) # you can call it like this 

secret_word = word_selection() # or store the value first 
guess_word(secret_word)   # and then pass it to your function 

寫程序一樣,在Python中,你通過後加入括號()叫什麼可能是一個很好的練習,看看這些東西是如何工作的 - 但你可以通過做這樣的事情來實現它:

WORD_LIST = ['python', 'something', 'completely', 'different'] 


def guess_word(word): 
    print('Time to guess the word:', '*'*len(word)) 

guess_word(random.choice(WORD_LIST)) 
+0

對不起,遲了回覆但該死的,這幫了我很多,謝謝! –

+0

@Haashim在SO上表達這種情緒的普遍接受的方式是upvote(頂部的三角形),如果它最好地回答你的問題,接受(點擊複選標記,它會變成綠色)。而且,不客氣:) –

-1

將返回值存儲在變量中。

import random 

print("Welcome to Hangman!") 

def wordSelection(): 
    words = ["test", "random"] 
    wordChoice = random.randint(0, 1) 
    selectedWord = words[wordChoice] 
    return selectedWord 

def guessWord(selectedWord): 
    selectedWord = wordSelection() 
    print selectedWord 
+0

當我看到一個以「嘗試這個」開頭的答案,後面跟着一段代碼,並沒有解釋代碼應該完成的事情時,我總是低估它。 – nbro

+0

@nbro,我會保重從此 – Surajano

0

您在返回從wordSelection()selectedWord值,因此你可以直接使用它在你的代碼。雖然,我不明白你想用這個做什麼guessWord功能是多餘的。

你可以做這樣的事情:

guessWord(wordSelection()) 

,並定義guessWord功能是這樣的:

guessWord(words) : 

這將路過的selectedWordwordSelection()即值函數guessWord()返回的值。

相關問題