1

This is a general question.

How can you return more than 1 value from a function, is it possible? for example, I am making a score counter in an arbitrary game, i have three variables:

  1. my_score
  2. comp_score
  3. tied

Next, i have a function in which there are various if, elif conditions depending on which any of the above variables is updated.

I call the function and the variables are updated accordingly, but now i want a print statement printing all three variables under score like,

print(" Score: Mine = {} , Comp = {} , Tied = {} ".format()) 
/*I need to enter three variables here inside .format() of which any one is updated and returned from the function according to the condition. But calling the function returns only one value*/

What should i do to make this possible?

Also again, the counter keeps starting from 0 again when the function is called as i initialized the three variables to 0 at the start.What do i do to make the counter keep on updating the values and printing three of them correctly?

Thanks in advance!

  • You can return [touple](https://docs.python.org/3.3/tutorial/datastructures.html#tuples-and-sequences) or [dictionary](https://docs.python.org/3.3/tutorial/datastructures.html#dictionaries) – janisz May 20 '15 at 06:43
  • returning a tuple gives us something like (1,1,1)..how can i use the elements of this tuple and print them separately . – saripalli sreekar May 20 '15 at 06:45
  • Take a look at [this answer](http://stackoverflow.com/a/539106/1387612) – janisz May 20 '15 at 06:46
  • again.can you answer the other part of the question? If you need the code, let me know.It's a little big. – saripalli sreekar May 20 '15 at 10:01
  • Can you post example how your function should work – janisz May 20 '15 at 11:57
  • i am using this code, https://gist.github.com/42dc5581661b3d6de169. The problem is that the score counter keeps on starting from 0 every time the program is run.i used a lot of functions in here, maybe in excess. The thing is that i know that this is the problem, but i don't know what to do.I want the counter to keep updating it's previous score every time the function is run. How do i do it? – saripalli sreekar May 21 '15 at 05:51
  • Basically you want to create function that keep it's state between executions. You can use [global variables](http://stackoverflow.com/a/423596/1387612) or [static variable in function](http://stackoverflow.com/q/279561/1387612) – janisz May 21 '15 at 06:51
  • i'm still finding the concept of static variables in python confusing.would you care to edit my code and post me the link to get the desired result if i'm not asking too much?thanks, greatly appreciate it. – saripalli sreekar May 22 '15 at 06:14
  • also can you comment out any inefficiencies in my code or just briefly tell me alternatives?thanks a ton! – saripalli sreekar May 22 '15 at 06:15

0 Answers0