#πŸ”’ How is parameter "prompt" being used?

50 messages Β· Page 1 of 1 (latest)

lost geyser
#

So I'm confused on how prompt is being "extracted" from the get_int function to the main function once ran. is "x" a global variable that is connecting the two functions?

Code:

def main():
x = get_int("Assign a value to x: ")
print(f"x is {x}")

def get_int(prompt):
while True:
try:
x = int(input(prompt))
except ValueError: # if the user gives a unsupported input
print("The assigned value of x is not a number. ")
else: # if there is no Value error and continue with line 23
break
return x

main()

polar rootBOT
#

@lost geyser

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.

north charm
#

!return-gif

polar rootBOT
#
Print and return

Here's a handy animation demonstrating how print and return differ in behavior.

See also: /tag return

lost geyser
north charm
#

What part aren't you getting?

lost geyser
#

so in the get_int function. there is a parameter "prompt". and in the get_int function, there is "x = int(input(prompt))". How is the get_int function grabbing the main function print statement?

#

and replacing it with "prompt"

north charm
#

The string inside of the () after get_int are the parameter(s) to the function

#

They get passed to the function as data

#

So prompt gets assigned the value "Assign a value to x:"

lost geyser
#

so x is a global variable?

north charm
#

No

lost geyser
#

so how are we returning the string associated with x in the main function

north charm
#

Your function is returning x which "passes" it back to main

#

x = get_int("...")

#

You could replace x with anything in the above and it would function the same

#

It's just coincidence that main is using x and get_int is using x

#

When get_int executed it's return value takes the place of the function call in the code

#

Get_int returns the value of x; so that's what is returned

#

But nothing requires you to call the variable x inside of main

silent loom
#
def main():
    x = get_int("Assign a value to x: ")
    print(f"x is {x}")



def get_int(prompt):
    while True:
        try:
            x = int(input(prompt))
        except ValueError: # if the user gives a unsupported input
            print("The assigned value of x is not a number. ")
        else: # if there is no Value error and continue with line 23
            break
    return x

in here

x = get_int("Assign a value to x: ")

"Assign a value to x: " is the value you are giving to the function get_int as the prompt parameter

input(prompt)

you are then passing the prompt variable into the input function which then prints the given string (in this case prompt) onto the screen / console and waits for you to give some input

and once it gives back that input you are trying to convert that input into an integer and if it is successfull you give that integer back as an output (with return x) which is then stored into the variable in the main function named x

solar holly
#

maybe this helps

silent loom
lost geyser
#

Jesus... im just not getting it. First week with python and let alone programming... Just cant wrap my head around it.

#

Thank you all for your time, though

solar holly
#

have you done any basic algebra? its the same concept as something like f(x) = x^2

#

the function describes the behavior that it squares any number you give it

#

to get any one specific output, you need to give it a value for x

lost geyser
#

wait so when get_int returns x.. its passed into x on the main function?

solar holly
#

in the same way,

def func(parameter):
  return parameter + 1

says that it will add 1 to any parameter you give it, but in order to get a specific output from it, we need to call it with a given parameter, ie func(10) or func(3)

solar holly
#

the x in get_int is not the same as the x in main.

solar holly
#

thats exactly right

lost geyser
solar holly
#

but its not necessarily because the variable is called x in get_int

solar holly
#

you know how you do something like answer = input()?

#

input is itself a function which "returns" a string

#

that "return value" is stored inside of answer because you call it in this way

#

func() is just an expression that evaluates to the return of func()

#

in the same way that (x + 3) is an expression that evaluates to whatever x + 3 is

#

in other words,

def func():
  return 10

print( func() )
print( 10 )

these give the same thing, because in this case, func() and 10 are interchangeable

silent loom
# lost geyser its just that im not seeing how there is any sort of calling from the main funct...

imagine this
if you say x = 2 + 2
the value stored in x will be 4 right? which is the result of the output of the addition operation instead of both the numbers and the +

similarly x = get_int(...) imagine the get_int(...) function call here gets replaced by the output of the function (aka returned value)
so what we are storing in the x variable using = in the main function is actually what we get out of the get_int function

lost geyser
#

I get it now. Thank you to you both. Be well

#

!close

polar rootBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, 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.