Where to add variable in a def function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Where to add variable in a def function

This is just an example ! I want to print the sum of all energy_level in the code below..so where do i place initial value for variable ' energy_level' i.e energy_level = 0 ?? https://code.sololearn.com/crl4cv8v9S43/?ref=app

24th Aug 2021, 4:04 PM
Sacar
Sacar - avatar
17 Answers
+ 3
You can either make info a global variable or make the function return
24th Aug 2021, 4:07 PM
Lisa
Lisa - avatar
+ 2
Sacar What about to make separate functions then calculate whatever you want like this: https://code.sololearn.com/cMEK9kYhF1R9/?ref=app
24th Aug 2021, 4:55 PM
A͢J
A͢J - avatar
+ 2
Sacar How about this one? :- info = {} revenue1 = 0 def ticket_sold(ticket_num, age): global info, revenue1 info[ticket_num] = age if age < 18: revenue1 += 5 else: revenue1 += 20 while True: try: ticket_num = input() age = int(input()) except EOFError: break ticket_sold(ticket_num, age) print(revenue1)
24th Aug 2021, 4:56 PM
Calvin Thomas
Calvin Thomas - avatar
+ 2
Cat-licious you don't need to include info after global keyword https://code.sololearn.com/cHcIuWB4t9JM/?ref=app Doesn't this work i don't undrestand
24th Aug 2021, 5:01 PM
Abs Sh
Abs Sh - avatar
+ 1
You want to fill the info dictionary with users, do I understand you correctly?
24th Aug 2021, 4:23 PM
Lisa
Lisa - avatar
+ 1
Sacar If you mean making a static variable, then the best thing you could do is to make a global variable. This should help: energy_level = 0 def foo(): # The 'global' keyword allows you to access and modify the variables that aren't inside the present scope global energy_level # I hope that this helps. Happy coding!
24th Aug 2021, 4:29 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Cat-licious you mean like this ? info = {} energy_level = 0 def user_info(name, age): global energy_level info[name] = age if age < 18: energy_level += 5 else: energy_level += 20 print(energy_level) while True: try: name = str(input()) age = int(input()) except EOFError: break else: user_info(name, age) Doesn't work !! Output is always 0
24th Aug 2021, 4:47 PM
Sacar
Sacar - avatar
+ 1
If you make it global all of values sum up but if make it local you have only 20 or 5 since local variables get deleted out of function scope
24th Aug 2021, 4:50 PM
Abs Sh
Abs Sh - avatar
+ 1
Cat-licious I get it now! Thank you bro 😊
24th Aug 2021, 5:06 PM
Sacar
Sacar - avatar
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Thank you bruh, that's helpful
24th Aug 2021, 5:10 PM
Sacar
Sacar - avatar
+ 1
Abs Sh Oh, you're right, as the 'info' variable by itself doesn't get modified. Assignment to the reference is always possible, so yeah, the global keyword isn't necessary for that case. Thanks for the correction! P.S. I'm confused once again. Suppise this code: dict = {7: 3} Here, is dict[7] the reference to the corresponding placeholder of 7 or just the reference to the int object '3'?
24th Aug 2021, 5:12 PM
Calvin Thomas
Calvin Thomas - avatar
+ 1
One solution is given already, and might be the least memory intensive ig, but another one I sometimes use, for reluctance to using a global variable, is to define it as a normal variable outside the scope of the function, then put it as a parameter for the function, note the most important thing, the parameter copies the value the argument gives it, not the argument itself, so you can't really update the normal variable, unless you return it from function and assign the return value to the original variable..
24th Aug 2021, 6:06 PM
scientist
scientist - avatar
+ 1
Another way is to make revenue a list: revenue = [0] def foo(): revenue[0] += 5 ...
25th Aug 2021, 2:30 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin you're a genius !
25th Aug 2021, 2:51 AM
Sacar
Sacar - avatar
+ 1
Sacar Thank you.
25th Aug 2021, 2:52 AM
Calvin Thomas
Calvin Thomas - avatar
0
Lisa sorry i don't get it how you return it in the function ? If i give 6 inputs like: Sagar 23 Lisa 34 Ram 12 Function returns only the last age i.e 12, i think..
24th Aug 2021, 4:19 PM
Sacar
Sacar - avatar
0
I'm open to suggestions and corrections corresponding to the interpretation though, happy learning!!
24th Aug 2021, 6:07 PM
scientist
scientist - avatar