#๐Ÿ”’ dodoegg

80 messages ยท Page 1 of 1 (latest)

muted canopy
#

need help

jagged geyserBOT
#

@muted canopy

Python help channel opened

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.

muted canopy
#

hello

crisp python
# muted canopy 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

prime fractal
#

@muted canopy Please edit your OP to include some context of your problem.

muted canopy
#

hello

#

okay

prime fractal
#

Include example code

muted canopy
#
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

prime fractal
#

create a function to call input and check the value

muted canopy
#

there is a problem with that

prime fractal
#

it should accept a prompt string which it forwards to input

muted canopy
#

the problem is that i dont want to hardcode and callthe function under every print statement

prime fractal
#

That's the use-case of functions.

muted canopy
#

okay

#

but is there a better way

prime fractal
#

You want some magic way to do while keeping input?

muted canopy
#

no. i just want to make it so if a user inputs something, thefunction is called

prime fractal
#

Go ahead and try to write a function that does the logic you need done.

muted canopy
#

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

prime fractal
#

Functions do that automatically. You can use return to pass data to the caller.

digital moon
#

js call the function each time for

muted canopy
prime fractal
#

So instead of name1=input("input a Proper Noun (name): "), you would do name1=prompt("input a Proper Noun (name): ")

prime fractal
muted canopy
#

so it returns to where the user was in the function

prime fractal
#

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.

muted canopy
#

okay. but what if it is invalid. how do i make the user type in the input a second time if it is

prime fractal
#

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)

muted canopy
prime fractal
#

prompt doesn't exist (yet). it's the name of the function you've yet to create.

muted canopy
#

oh. i though it was built in. but why not just use input?

prime fractal
#

a basic impl would look like this with no validation. ```py
def prompt(question):
response = input(question)

do validation here

return response

muted canopy
#

so validation is where you make the user input a proper value

prime fractal
#

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"

muted canopy
#

what would i do if it wasn't

prime fractal
#

Have you done loops yet?

muted canopy
#

not really. like once before

prime fractal
#

You can use an infinite loop (while True:) and only break/return when the user gives valid input

muted canopy
#

give me example code for reference later when i am struggling on another project

prime fractal
#
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

muted canopy
#

okay. if it is invalid prompt will be called again

prime fractal
#

Yup. If you want, you can print some message when the input is invalid.

muted canopy
#

okay. so should i call prompt() every time they input something

prime fractal
#

yes.

muted canopy
#

would that be hardcoding

prime fractal
#

How do you define hard coding?

muted canopy
#

coding in the least efficient way-including extra, repeated lines of code

prime fractal
#

So the opposite of DRY (Don't Repeat Yourself)

muted canopy
#

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

prime fractal
#

To me, hard coding is more like coding something in a way that isn't configurable or extensible.

muted canopy
#

okay

#

one last thing

prime fractal
#

You have hard coded your prompts and story because you can't customize it.

muted canopy
#

how can i not hardcode my prompts

prime fractal
#

provide your word list and story strings as function parameters, then format the story with str.format

muted canopy
#

can you give an example

prime fractal
#

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}.
"""
)

muted canopy
#

did you mean to define

#

gathering

prime fractal
#

I've omitted the def

muted canopy
#

but would that be a def f you didn't

prime fractal
#

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

muted canopy
#

parameter?

prime fractal
#

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

muted canopy
#

what would i put in place of question here response = input(question)

#

?

#

I'll work now with this information

#

thank you

jagged geyserBOT
#
Python help channel closed for inactivity

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.