I'm just starting to learn Python 3.9 as my first language. I have been fighting with this error for a while now, and I can't figure out what the issue is.
Here's what I'm working on:
def eval_express(eqstring[0], eqstring[1], eqstring[2]):
eqstring[0], eqstring[2] = float(eqstring[0]), float(eqstring[2])
return opdict[eqstring[1]](eqstring[0], eqstring[2])
I'm receiving an error that the "(" after eval_express is not closed, but as far as I can tell it is. At first, I thought it was just a glitch, but despite numerous attempts to rewrite it, increase/decrease the number of arguments, etc. it persisted. The error cropped up after I modified the arguments from variables to list items, but I don't see why that would affect it. Can anyone provide some clarification on what the program's getting hung up on?
Thank you for your help!
You are using square brackets inside the function parameters, which is not valid. Valid code would be:
def eval_express(eqstring0, eqstring1, eqstring2):
eqstring0, eqstring2 = float(eqstring0), float(eqstring2)
return opdict[eqstring1](eqstring0, eqstring2)
although you should probably use more descriptive parameter names.
You can't use parameter[] notation when entering a parameter to a function. Instead just use parameter, or you will have to do something like.
def eval_express(eqstring):
eqstring[0], eqstring[2] = float(eqstring[0]), float(eqstring[2])
return opdict[eqstring[1]](eqstring[0], eqstring[2])
Now you have to pass an array as the function parameter.
Related
I want to understand the steps Python is taking to go through the nested function below:
def raise_val(n):
def inner(x):
raised = x ** n
return raised
return inner
cube = raise_val(3)
print(cube(5))
125
I am trying to wrap my head around how this works as I cannot seem to figure it out.
Why doesn't Python give any value if you just do print(raise_val(any #)), and how does setting cube = raise_val(any #) make cube a function? I don't understand how when you initially put a number in raise_val, such as raise_val(3), it makes n = 3 within the inner(x) function, but can still return a value to store even when x is still empty. When you do cube = raise_val(3), and then follow it with cube(5), how does that make x = 5 within the inner(x) function, so that the variable raised = 5 ** 3? How does it know which variable to fill with which number input?
I am confused but I truly want to understand as I feel like this is something I need to know in order to be proficient in Python. This is my first question ever posted so I hope I explained it clearly. Any insight would be greatly appreciated, thank you!
I tried playing around with the code, trying different inputs, but still cannot understand the process. I can memorize it, but I want to be able to do more than that!
First, I think another way to write this function is by using one function with two parameters (n and x). Another error is you are naming the value of "raising" when you call the raise_val function, which should be an integer. Therefore, you can't use it as a function again like cube(5). Here is my code. I hope this help!
def raise_val(n,x):
return x**n
print(raise_val(3,5))
Iv'e tried searching up the problem to no avail, so I chose to come on stack overflow to ask this problem.
def Year(2015):
^
Syntax Right here
Year = 2015
return Year
Year = '2015'
print(Year)
I am not sure what are you intending to do, but what you seem to intend to do is to define Year(...) as function. Well, in Python when defining function you need to put in brackets a parameter that it would be used somewhere inside the function.
Def Year(Year_Param): # Do stuff return Year_Param
So in this is how a function is defined in Python, where Year_Param is a Parameter that you give when calling the function Year().
If what you are looking for is that the function Year uses default value for a Parameter in case it is called without Parameters, then you shoud write def Year(Year_Param = 2015): # Your stuffs here
In this case, if you call Year() like this without putting anything in middle of the brackets it would automatically be like Year(2015)
I'm learning Python and, so far, I absolutely love it. Everything about it.
I just have one question about a seeming inconsistency in function returns, and I'm interested in learning the logic behind the rule.
If I'm returning a literal or variable in a function return, no parentheses are needed:
def fun_with_functions(a, b):
total = a + b
return total
However, when I'm returning the result of another function call, the function is wrapped around a set of parentheses. To wit:
def lets_have_fun():
return(fun_with_functions(42, 9000))
This is, at least, the way I've been taught, using the A Smarter Way to Learn Python book. I came across this discrepancy and it was given without an explanation. You can see the online exercise here (skip to Exercize 10).
Can someone explain to me why this is necessary? Is it even necessary in the first place? And are there other similar variations in parenthetical syntax that I should be aware of?
Edit: I've rephrased the title of my question to reflect the responses. Returning a result within parentheses is not mandatory, as I originally thought, but it is generally considered best practice, as I have now learned.
It's not necessary. The parentheses are used for several reason, one reason it's for code style:
example = some_really_long_function_name() or another_really_function_name()
so you can use:
example = (some_really_long_function_name()
or
another_really_function_name())
Another use it's like in maths, to force evaluation precede. So you want to ensure the excute between parenthese before. I imagine that the functions return the result of another one, it's just best practice to ensure the execution of the first one but it's no necessary.
I don't think it is mandatory. Tried in both python2 and python3, and a without function defined without parentheses in lets_have_fun() return clause works just fine. So as jack6e says, it's just a preference.
if you
return ("something,) # , is important, the ( ) are optional, thx #roganjosh
you are returning a tuple.
If you are returning
return someFunction(4,9)
or
return (someFunction(4,9))
makes no difference. To test, use:
def f(i,g):
return i * g
def r():
return f(4,6)
def q():
return (f(4,6))
print (type(r()))
print (type(q()))
Output:
<type 'int'>
<type 'int'>
I am using codio and ran into a challenge that doesn't make sense to me.
It says "Your code should expect one input. All you need to do is add 12 to it and output the result".
I am new to Python and was wondering if someone could explain what this means. I can't seem to find any information anywhere else.
This is asking you to make a function which adds 12 to any number you input.
This could be done as follows:
def add_12(num):
return num+12
It's a task definition. You have to write a function which accepts one input parameter, add 12 to it, and return back to caller.
Your solution is as below:
print(N+12)
I want to make it so it prints different hints dependent on where the player is in the game. I tried it by setting the value of 'Hint' every time the player went somewhere. Obviously I came across a flaw (as I'm here). The value of Hint = 1 was first in one def and I couldn't manage to summon it when writing the Help/Hint def. My pathetic example:
def Room_Choice():
Hint = 1
(60 lines down)
def Hint():
Choice = input("What would you like?\n")
if Choice == ("Hint"):
if Room_Choice(Hint = 1):
print_normal("blah blah blah\n")
else:
print_normal("HINT-ERROR!\n")
Help_Short()
And obviously as the game developed more and more values of hint would be added.
As you can see I'm relatively new to Python and need help.
You are trying to reach a value that exists in a function scope, and you're doing it wrong (as you're here).
Imagine scopes as boxes of one-way mirrors : when you're inside one, you can see what's in the box and what's outside of the box. But you can't see what's in a box you are not in.
Here, Hint exists within the box of Room_Choice, but not in the box of H... oh wait.
You've called your function Hint too ! If you want to reach Hint in a function called Hint with no Hint defined inside the function, you'll probably get the function. Let's call the function DoHint()
So you can't see Hint from within DoHint, because it's in another box. You have to put it somewhere (over the rainboooow... sorry for that) you can see it.
You might want to put it at the module level (not within a def), or make it an object's attribute (but you'll have to know bits of Oriented Object Programming to understand that).
This is really basic programming skills, I can't really explain further without knowing what you're trying to do and showing you how I would do it, but I hope that helped.
Just one more thing on this line : if Room_Choice(Hint = 1):, here you're trying to check if the result of the Room_Choice function with a value of 1 for the Hint parameter is True. I don't know what you wanted to do, but the Room_Choice function doesn't show it can handle any parameters (you should get an error on that), and will not return a boolean value (it will implicitly return None, which is evaluated as a logical False).