0

This function that I have written thus:

def simulate_turn(num_rolls, score, opponent_score):
    """This function takes in two scores and a number of die rolls and returns
    what the two scores would be if num_rolls many dice were rolled. This takes
    into account the swine swap, free bacon, and hog-wild."""
    x = score
    y = opponent_score
    x += take_turn(num_rolls,opponent_score,select_dice(score,opponent_score))
    if ifwillswap(x,y):
        swap(x,y)
    return x,y

When run in the interactive python shell (the function comes from a .py file), it is returning an int object instead of a tuple! What am I doing wrong? I am trying to have it turn a pair of values, not a single int object.

5
  • 1
    provide your IDLE transcript so we can see exactly what's going on Commented Sep 12, 2013 at 3:30
  • 3
    I just mocked up this function, replacing take_turn and other undefined things with lambda *a: 1. It returns a tuple. (There's no way this function shown is returning anything other than a tuple. That return statement returns a tuple.) Commented Sep 12, 2013 at 3:31
  • What int object is it returning? The value of x or the value of y? Commented Sep 12, 2013 at 3:36
  • 2
    There's no way swap works. (Are you taking CS61A at UCB?) Commented Sep 12, 2013 at 3:40
  • 1
    "it is returning an int object instead of a tuple! " - I don't believe that. Commented Sep 12, 2013 at 4:06

3 Answers 3

1

You already do return a pair of values. Even if you somehow broke x and y somewhere along the way and had something ridiculous like this:

def example():
    return None, None
a = example()

a would still hold a reference to the tuple (None, None) after the execution of that function. So you are returning a tuple of two "somethings", the only question is what are those "somethings" and how you are storing them. There is no reason for you to think that your function returns an int however, because it doesn't. No matter what, with the syntax you've used, your function returns a tuple of two types. You could even just do return x, and that would return a one-item tuple. The comma prevents you from returning just an int.

Sign up to request clarification or add additional context in comments.

Comments

0

My guess is that your function swap() is maybe setting your variable y equal to None and that you are perhaps misinterpreting the returned values. As others have said, I don't see how you can see anything but a tuple as the return.

Comments

-2
from typing import Tuple
(int,int)-> Tuple
(int,str)_.Tuple

By using this code from above you can return a tuple.

I am using the Tuple function which works well. Hope this helps.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.