Unresolved reference in defined functionCalling a function of a module by using its name (a string)How to flush output of print function?Using global variables in a functionHow to make a chain of function decorators?How do I pass a variable by reference?Peak detection in a 2D arrayReading a .txt into a definitionHow do you define __str__ in python when including newline characters?Unresolved reference issue in PyCharmPython: How to update list with two elements
Professor refuses to write a recommendation letter to students who haven't written a research paper with him
How to make a pipe-divided tuple?
Filling attribute tables with values from the same attribute table
Why are some hotels asking you to book through Booking.com instead of matching the price at the front desk?
Python reimplementation of Lost In Space by Tim Hartnell
How do draw effects during the discard phase work?
Do 643,000 Americans go bankrupt every year due to medical bills?
Why can't some airports handle heavy aircraft while others do it easily (same runway length)?
Why is it that I have to play this note on the piano as A sharp?
What's this inadvertent thing?
What does "先が気になる" mean?
How can electricity be positive when electrons are negative?
Friend is very nit picky about side comments I don't intend to be taken too seriously
Explanation of switch statement constraints on variably modified types in C standard
Examples where "thin + thin = nice and thick"
What is the "Brake to Exit" feature on the Boeing 777X?
When does order matter in probability?
Can taking my 1-week-old on a 6-7 hours journey in the car lead to medical complications?
Project Euler Problem 45
What is the extent of the commands a Cambion can issue through Fiendish Charm?
Why is Sojdlg123aljg a common password?
Is it right to use the ideas of non-winning designers in a design contest?
k times Fold with 3 changing extra variables
What's in a druid's grove?
Unresolved reference in defined function
Calling a function of a module by using its name (a string)How to flush output of print function?Using global variables in a functionHow to make a chain of function decorators?How do I pass a variable by reference?Peak detection in a 2D arrayReading a .txt into a definitionHow do you define __str__ in python when including newline characters?Unresolved reference issue in PyCharmPython: How to update list with two elements
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to call a function that I defined in the code, however, it's saying that it's not defined? The error is where I have "add_to()" it's saying it's not defined. What am I doing wrong here?
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
print(grocery_list)
question = input("Would you like to add anything to the list?: ")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
def add_to():
input("Please enter the item you'd like to add: ")
grocery_list.append(str(input))
print(grocery_list)
python python-3.x
add a comment |
I'm trying to call a function that I defined in the code, however, it's saying that it's not defined? The error is where I have "add_to()" it's saying it's not defined. What am I doing wrong here?
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
print(grocery_list)
question = input("Would you like to add anything to the list?: ")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
def add_to():
input("Please enter the item you'd like to add: ")
grocery_list.append(str(input))
print(grocery_list)
python python-3.x
Put the definition ofadd_toat the top.
– Loocid
Mar 28 at 6:00
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07
add a comment |
I'm trying to call a function that I defined in the code, however, it's saying that it's not defined? The error is where I have "add_to()" it's saying it's not defined. What am I doing wrong here?
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
print(grocery_list)
question = input("Would you like to add anything to the list?: ")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
def add_to():
input("Please enter the item you'd like to add: ")
grocery_list.append(str(input))
print(grocery_list)
python python-3.x
I'm trying to call a function that I defined in the code, however, it's saying that it's not defined? The error is where I have "add_to()" it's saying it's not defined. What am I doing wrong here?
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
print(grocery_list)
question = input("Would you like to add anything to the list?: ")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
def add_to():
input("Please enter the item you'd like to add: ")
grocery_list.append(str(input))
print(grocery_list)
python python-3.x
python python-3.x
edited Mar 28 at 7:05
Rachit kapadia
5756 silver badges18 bronze badges
5756 silver badges18 bronze badges
asked Mar 28 at 5:58
MrSMrS
213 bronze badges
213 bronze badges
Put the definition ofadd_toat the top.
– Loocid
Mar 28 at 6:00
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07
add a comment |
Put the definition ofadd_toat the top.
– Loocid
Mar 28 at 6:00
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07
Put the definition of
add_to at the top.– Loocid
Mar 28 at 6:00
Put the definition of
add_to at the top.– Loocid
Mar 28 at 6:00
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07
add a comment |
1 Answer
1
active
oldest
votes
You did function declaration after the function call. Please follow :PEP8 for more information and secondly if take any input from user, you need to store in some variable to use any way. Here is the code that add item perfectly.
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
def add_to():
s= input("Please enter the item you'd like to add: n")
grocery_list.append(str(s))
print(grocery_list)
question = input("Would you like to add anything to the list?: n")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
print(grocery_list)
Output :
['salmon', 'beef', 'eggs', 'milk']
Would you like to add anything to the list?:
yes
Please enter the item you'd like to add:
yourhead
['salmon', 'beef', 'eggs', 'milk', 'yourhead']
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okaynis for good code readability. You need to givenwithinput()and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to given
– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391020%2funresolved-reference-in-defined-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You did function declaration after the function call. Please follow :PEP8 for more information and secondly if take any input from user, you need to store in some variable to use any way. Here is the code that add item perfectly.
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
def add_to():
s= input("Please enter the item you'd like to add: n")
grocery_list.append(str(s))
print(grocery_list)
question = input("Would you like to add anything to the list?: n")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
print(grocery_list)
Output :
['salmon', 'beef', 'eggs', 'milk']
Would you like to add anything to the list?:
yes
Please enter the item you'd like to add:
yourhead
['salmon', 'beef', 'eggs', 'milk', 'yourhead']
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okaynis for good code readability. You need to givenwithinput()and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to given
– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
add a comment |
You did function declaration after the function call. Please follow :PEP8 for more information and secondly if take any input from user, you need to store in some variable to use any way. Here is the code that add item perfectly.
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
def add_to():
s= input("Please enter the item you'd like to add: n")
grocery_list.append(str(s))
print(grocery_list)
question = input("Would you like to add anything to the list?: n")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
print(grocery_list)
Output :
['salmon', 'beef', 'eggs', 'milk']
Would you like to add anything to the list?:
yes
Please enter the item you'd like to add:
yourhead
['salmon', 'beef', 'eggs', 'milk', 'yourhead']
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okaynis for good code readability. You need to givenwithinput()and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to given
– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
add a comment |
You did function declaration after the function call. Please follow :PEP8 for more information and secondly if take any input from user, you need to store in some variable to use any way. Here is the code that add item perfectly.
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
def add_to():
s= input("Please enter the item you'd like to add: n")
grocery_list.append(str(s))
print(grocery_list)
question = input("Would you like to add anything to the list?: n")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
print(grocery_list)
Output :
['salmon', 'beef', 'eggs', 'milk']
Would you like to add anything to the list?:
yes
Please enter the item you'd like to add:
yourhead
['salmon', 'beef', 'eggs', 'milk', 'yourhead']
You did function declaration after the function call. Please follow :PEP8 for more information and secondly if take any input from user, you need to store in some variable to use any way. Here is the code that add item perfectly.
grocery_list = ['salmon', 'beef', 'eggs', 'milk']
def add_to():
s= input("Please enter the item you'd like to add: n")
grocery_list.append(str(s))
print(grocery_list)
question = input("Would you like to add anything to the list?: n")
if question == "yes" or "y" or "Y":
add_to()
else:
print("Enjoy your shopping")
print(grocery_list)
Output :
['salmon', 'beef', 'eggs', 'milk']
Would you like to add anything to the list?:
yes
Please enter the item you'd like to add:
yourhead
['salmon', 'beef', 'eggs', 'milk', 'yourhead']
edited Mar 28 at 6:08
answered Mar 28 at 6:02
Rachit kapadiaRachit kapadia
5756 silver badges18 bronze badges
5756 silver badges18 bronze badges
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okaynis for good code readability. You need to givenwithinput()and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to given
– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
add a comment |
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okaynis for good code readability. You need to givenwithinput()and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to given
– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
That's another thing I wasn't struggling to figure out. Thank you! Can you explain why I need to add the (str part? Also what's the point of using n to start a new line when I don't need a new line? Is it just good in practice?
– MrS
Mar 28 at 6:11
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Had to wait to be able to do it. Just did both, thanks.
– MrS
Mar 28 at 6:14
Thanks, Okay
n is for good code readability. You need to give n with input() and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to give n– Rachit kapadia
Mar 28 at 6:18
Thanks, Okay
n is for good code readability. You need to give n with input() and lets say in future if their is more confusing input user is giving which is long input and mixture of string and int, then you have to give n– Rachit kapadia
Mar 28 at 6:18
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Okay and the point of (str) is just to let it know that they are inputting text and that it can only be text, correct?
– MrS
Mar 28 at 6:23
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
Its depend on developer(you), whether in which form you want to grab input from the user whether in form of int, float, string..etc
– Rachit kapadia
Mar 28 at 6:25
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55391020%2funresolved-reference-in-defined-function%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Put the definition of
add_toat the top.– Loocid
Mar 28 at 6:00
Wow, I feel stupid. thank you
– MrS
Mar 28 at 6:07