#๐ dodoegg
80 messages ยท Page 1 of 1 (latest)
@muted canopy
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
hello
Please describe the issue and show related parts of code along with what you expect to happen and what is actually happening, and a possible traceback (full). And read the above embed, so someone who can help has some grounds to start from
@muted canopy Please edit your OP to include some context of your problem.
Include example code
def gathering():
name1=input("input a Proper Noun (name): ")
name2=input("input a Proper Noun (name): ")
name3=input("input a Proper Noun (name): ")
adjective1=input("input an adjective: ")
adjective2=input("input an adjective: ")
verb1=input("input a past tense verb: ")
noun0=input("input a noun (living creature or person-can be made up)-singular: ")
noun1=input("input a noun (living creature or person-can be made up)-plural:
verb2=input("input a past tense verb: ")
noun2=input("input a noun (living creature or person-can be made up)-singular: ")
verb3=input("input a progressive verb: ")
verb4=input("input a verb targetted towards your {noun2}: ")
verb5=input(f"input a verb targetted towards your {noun1}")
print(f"Once upon a time there were 3 elves. They were named {name1}, {name2}, and {name3}. ")
print(f"The elves were {adjective1} and {adjective2} and people {verb1} on them. One day a mighty ")
print(f"{noun0} and with his wand {verb2} them. The elves then awoke as {noun1} and {verb3}")
print(f" {noun2} that was {verb3}. The {noun1} {verb4} the {noun2} and the {noun2} {verb5} them.")
print(f"The {noun1} died. The end")
gathering()```
i don't want the user to input empty strings
create a function to call input and check the value
there is a problem with that
it should accept a prompt string which it forwards to input
the problem is that i dont want to hardcode and callthe function under every print statement
That's the use-case of functions.
You want some magic way to do while keeping input?
no. i just want to make it so if a user inputs something, thefunction is called
Go ahead and try to write a function that does the logic you need done.
there is one more thing
how do i, once a function is called, go back to the input statement i left off on in a seperate function. i don't want the user to have to input everything again after the other function that manages errors is called
Functions do that automatically. You can use return to pass data to the caller.
js call the function each time for
so return is like ctrl-Z or undo back to where you were
So instead of name1=input("input a Proper Noun (name): "), you would do name1=prompt("input a Proper Noun (name): ")
It's not an undo. It's a return.
so it returns to where the user was in the function
Yes. What line we are on is stored in what we call a stack. When you call a function, it will "push" (append) to the stack. Returning will "pop" (remove) from the stack.
But you won't need to memorize that.
It doesn't become too important until much later when you start making functions that call themselves.
okay. but what if it is invalid. how do i make the user type in the input a second time if it is
A loop comes in handy for that. Input until the user gives a valid string
Or you can just call the function again (function that calls itself)
how is prompt different or better than input
prompt doesn't exist (yet). it's the name of the function you've yet to create.
oh. i though it was built in. but why not just use input?
a basic impl would look like this with no validation. ```py
def prompt(question):
response = input(question)
do validation here
return response
so validation is where you make the user input a proper value
We don't want to "Just use input" because input doesn't provide the kind of validation you want.
validation is checking if some data is "valid"
what would i do if it wasn't
Have you done loops yet?
not really. like once before
You can use an infinite loop (while True:) and only break/return when the user gives valid input
give me example code for reference later when i am struggling on another project
def prompt(question):
while True:
response = input(question)
if response:
return response
# else: check failed, continue to next loop and input again
that will make them input something
okay. if it is invalid prompt will be called again
Yup. If you want, you can print some message when the input is invalid.
okay. so should i call prompt() every time they input something
yes.
would that be hardcoding
How do you define hard coding?
coding in the least efficient way-including extra, repeated lines of code
So the opposite of DRY (Don't Repeat Yourself)
don't repeat yourself is how i would define hardcoding as well as writing multiple lines of code to do something that could be done in one
To me, hard coding is more like coding something in a way that isn't configurable or extensible.
You have hard coded your prompts and story because you can't customize it.
how can i not hardcode my prompts
provide your word list and story strings as function parameters, then format the story with str.format
can you give an example
something like this. ```py
gathering(
{
"name1": "Proper Noun (name)",
"name2": "Proper Noun (name)",
"name3": "Proper Noun (name)",
},
"""
Once upon a time there were 3 elves. They were named {name1}, {name2}, and {name3}.
"""
)
I've omitted the def
but would that be a def f you didn't
You basically iterate the dict, prompting for each name, then calling param2.format(**names)
It would actually simplify the logic by getting rid of the data
can you explain param2
parameter?
Here's how I would implement it. ```py
def prompt(question):
while True:
response = input(question)
if response:
return response
def gathering(name_prompts, story):
names = {}
for name, name_prompt in name_prompts.items():
names[name] = prompt(f"input a {name_prompt}: ")
print(story.format(**names))
gathering(
{
"name1": "Proper Noun (name)",
"name2": "Proper Noun (name)",
"name3": "Proper Noun (name)",
"adjective1": "",
"adjective2": "",
"verb1": "",
"noun0": "",
"noun1": "",
"verb2": "",
"noun2": "",
"verb3": "",
"verb4": "",
"verb5": "",
},
"""
Once upon a time there were 3 elves. They were named {name1}, {name2}, and {name3}.
The elves were {adjective1} and {adjective2} and people {verb1} on them. One day a mighty
{noun0} and with his wand {verb2} them. The elves then awoke as {noun1} and {verb3}
{noun2} that was {verb3}. The {noun1} {verb4} the {noun2} and the {noun2} {verb5} them.
The {noun1} died. The end
"""
)
what would i put in place of question here response = input(question)
?
I'll work now with this information
thank you
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.