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)
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))
For example, in my Codecademy it says,
def spam():
print ("Eggs!")
but I feel like you could just print Eggs! if you wanted without the def spam():
Somebody please help
But you could do:
def spam():
print("Eggs!")
And then call spam a thousand times in your code. Now, if you want to change that to print "Bacon!" you only have to change it once, rather than a thousand times.
def spam():
print("Bacon!")
Def helps to make your code reusable. In this case, we can think that it's useless indeed. But in other case, you'll want to be able to call a part of code multiple time !
In this case you're right, there is no reason to create a function for just print, but when you get to more complex writing a function saves you a lot of precious time and space. For example I want to get a specific part of a .json with API, instead of writing all these lines again and again I will write it once and call it whenever I need it.
Best practice would be to define the string once and call the function as many times as needed. It will be easier to maintain. Yes, you can find and replace all instances, but you risk accidentally changing more than you bargained for. Additionally, if you were working on a shared project and someone were to merge code after you made that change, you’d have to go back and update all their new code to reflect the change. If they had just been calling spam(), there’s no update needed post-merge.
Usually it is for demonstration/illustration purposes. Once the code flow reaches the function, you'll get the message printed. Sometimes it is important to understand the sequence of calling the functions or just the fact that the function has been called.
Thank you for taking time to read this. I have a project due for my Programming class by Friday. The project is basically analyzing the most popular songs around the world. The user will input W is they want to see the most popular genre worldwide and how many streams it has. My program consists of two python files, one that contains the top 10 song list, and the other where the user will input their options.
Here is my file for my top10songs:
def Mostpopularw(Pop):
Pop =='Pop with 10,882,755,219 streams worldwide'
return Pop
and the file for where the user will put input
if choice=='W':
print(top10songs.Mostpopularw(Pop))
the code runs fine but when I try to enter 'W; it prints out
NameError: name 'Pop' is not defined
but I dont understand how pop is not defined? Can anyone help me?
Thanks!
It is not very clear what you want your Mostpopularw function to do, so its hard for us to help you make it do that thing. The current code doesn't make much sense, as you're comparing a Pop argument with a constant string, and then throwing away the result of the comparison before returning the same Pop value.
It may be that you just want the function to return the string, in which case it shouldn't be an argument:
def Mostpopularw():
Pop = 'Pop with 10,882,755,219 streams worldwide' # note, one = sign here!
return Pop
Now the calling code doesn't need to pass in an argument for Pop, which was the error you were having (you were passing in something that didn't exist).
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.
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).