+ 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
17 odpowiedzi
+ 3
You can either make info a global variable or make the function return
+ 2
Sacar
What about to make separate functions then calculate whatever you want like this:
https://code.sololearn.com/cMEK9kYhF1R9/?ref=app
+ 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)
+ 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
+ 1
You want to fill the info dictionary with users, do I understand you correctly?
+ 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!
+ 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
+ 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
+ 1
Cat-licious I get it now! Thank you bro 😊
+ 1
A͢J - S͟o͟l͟o͟H͟e͟l͟p͟e͟r͟ Thank you bruh, that's helpful
+ 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'?
+ 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..
+ 1
Another way is to make revenue a list:
revenue = [0]
def foo():
revenue[0] += 5
...
+ 1
Calvin you're a genius !
+ 1
Sacar Thank you.
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..
0
I'm open to suggestions and corrections corresponding to the interpretation though, happy learning!!