简体   繁体   English

如何从包含多个变量的函数中仅调用一个变量?

[英]How would I call only one variable from a function that contains multiple variables?

So basically I am trying to create a hangman game, and as of right now I'm just trying to pass a variable from one function to another, though I'm confused on how to do so, here is my code: 因此,基本上,我正在尝试创建一个子手游戏,截至目前,我只是试图将一个函数中的变量传递给另一个函数,尽管我对此却感到困惑,这是我的代码:

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

The question I have is basically, how am I able to call the variable 'selectedWord' from the wordSelection function and pass it into the guessWord() function? 我的问题基本上是,如何从wordSelection函数中调用变量'selectedWord'并将其传递给guessWord()函数?

I tried to at the bottom as you can see, however I just called the whole function again, instead of only specifically getting selectedWord rather than the whole function. 如您所见,我尝试在底部进行搜索,但是我只是再次调用了整个函数,而不是仅专门selectedWord selectWord而不是整个函数。

What could I do to ONLY get 'selectedWord' from WordSelection() rather than just getting the whole function - is this possible? 我只能从WordSelection()获取'selectedWord' ,而不仅仅是获取整个函数,该怎么办?这可能吗?

You are returning the selectedWord value from wordSelection() so you can just directly use it in your code. 您将从wordSelection()返回selectedWord值,因此可以直接在代码中直接使用它。 Although, i don't understand what are you trying to accomplish with this as guessWord function is redundant. 虽然,我不明白您要用这个来完成什么,因为guessWord函数是多余的。

You can do something like this : 您可以执行以下操作:

guessWord(wordSelection()) 

and define guessWord function like this : 并定义guessWord函数:

guessWord(words) :

This will pass the value returned by wordSelection() ie value of selectedWord in function guessWord() . 这将传递wordSelection()返回的值,即功能guessWord()selectedWord值。

You have a misunderstanding about how variables work, especially in Python. 您对变量的工作方式有误解,尤其是在Python中。

In Python, a variable is simply a label that refers to a value that exists somewhere in memory. 在Python中,变量只是一个标签,它引用内存中某个位置存在的值。 For example: 例如:

 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

When you return a value from a function, you're not returning the name - you're returning effectively where the value lives. 从函数返回值时,您没有返回名称-您实际上是在返回值所在的位置。 If you want to keep that value, you're going to have to give it a name: 如果要保留该值,则必须给它起一个名字:

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

So in your code, you want to call a function that gives you a random word, which you can do like this: 因此,在您的代码中,您想调用一个为您提供一个随机单词的函数,您可以像这样进行操作:

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)

In order to do anything with this value, you need to store it. 为了使用此值执行任何操作,您需要存储它。 It's returned wherever you call the function. 无论您在何处调用函数,都将返回它。 In Python you call something by adding parenthesis () after it: 在Python中,您可以通过在其后面添加括号()调用某些内容:

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

So you can either require an argument on your guess_word function and pass it in: 因此,您可以在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

Writing your program like that is probably a good exercise in seeing how that stuff works - but you could actually make it the same by doing something like this: 这样编写程序可能是了解这些东西如何工作的一个好习惯-但是您实际上可以通过执行以下操作使其相同:

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


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

guess_word(random.choice(WORD_LIST))

Store the return value in a variable. 将返回值存储在变量中。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM