0

Why can define not be used to define variables?

I'm trying to use the define function to create a series of variables. Each variable will be what the user inputs and will then be used in later code. The crux of the problem is shown below. I assumed I could define var in fnctn as input, and then substitute var with new variables (word1, word2, etc) when the function is recalled. But I get "NameError: name 'word1' is not defined" whenever I attempt to run it. If you can insert a string or an integer into a recalled defined function, why not a variable? And what could a workaround for this be? def fnctn(var): var = input("enter a word: ") fnctn(word1) fnctn(word2) etc.

3rd Mar 2020, 3:19 PM
Savager
1 Resposta
0
First, You can substitute value in function. Not variable. Second, You can reassign variable to new value. So, in your code, word1 & word2 must be defined before calling function. word1 = 'Hello' word2 = 'World' When you call fnctn function, this happen --> fnctn(var = word1 (or) word2) And you reassign that var to new value which is returned by input function. So, value contained in your 'word' variable will be never used.
3rd Mar 2020, 3:58 PM
The Sylar