How do i make the function "turam" not return a tuple?

def turam(pieniadze, tura, cdom, csklep, cfabryka, wolne, wybor, domy, sklepy, fabryki):
    print(' ')
    print('tura', tura)
    print('Pieniądze:', pieniadze, 'Mieszkańcy:', mieszkancy)
    print('Kup: 1. Domy:', cdom, '2. Sklepy:', csklep, '3. Fabryki:', cfabryka)
    wybor = int(input('Wybierz, co chcesz kupić (1-3): (wybierz 4 jeśli nie chcesz kupować niczego): '))
    if wybor == 1:
        pieniadze -= cdom
        tura += 1
        wolne -= 1
        domy += 1
        print('Pieniądze', pieniadze, 'Tura', tura,)
        print('Domy', domy, 'Wolne', wolne)
        return pieniadze,domy
    elif wybor == 2:
        pieniadze -= csklep
        tura += 1
        wolne -= 1
        sklepy += 1
        print('Pieniądze', pieniadze, 'Tura', tura,)
        print('Sklepy', sklepy, 'Wolne', wolne)
        return pieniadze,sklepy
    elif wybor == 3:
        pieniadze -= cfabryka
        tura += 1
        wolne -= 1
        fabryki += 1
        print('Pieniądze', pieniadze, 'Tura', tura,)
        print('Fabryki', fabryki, 'Wolne', wolne)
        return pieniadze,fabryki
    elif wybor == 4:
        tura += 1
        print('Pominięto turę')
        print('Pieniądze', pieniadze, 'Tura', tura,)
    else:
        print(' ')
        print('Błędna wartość!')
        turam(pieniadze, tura, cdom, csklep, cfabryka, wolne, wybor, domy, sklepy, fabryki)
    return pieniadze
if wybor == 1:
    while True:
        pieniadze = turam(pieniadze, tura, cdom, csklep, cfabryka, wolne, wybor, domy, sklepy, fabryki)
        tura += 1
        if pieniadze <= 0:
            print(' ')
            print('Zbankrutowałeś!')
            print('Skończyłeś grę z', mieszkancy, 'mieszkańcami,', domy, 'domami,', sklepy, 'sklepami i', fabryki, 'fabrykami.')
            raise KeyboardInterrupt 

I apologize if you have trouble reading the code, the variables are in Polish

The second code is for stopping the whole code when “pieniadze” is equal or below 0
What i want to do, is that when i input 1 2 or 3 the integer variables “pieniadze”,“domy”,“sklepy”,“fabryki” and “wolne” are updated, but after i input a number i get
" if pieniadze <= 0:
TypeError: ‘<=’ not supported between instances of ‘tuple’ and ‘int’ " error message

This (and the lines like it) is returning a tuple. If you don’t want to do that, return one variable.

1 Like

It sometimes returns a tuple because that’s how you’ve written it.

1 Like

I guess you want to update the state after each ‘turam()’. Returning the money and houses, shops, etc. will just return the numbers, not change the global state. You can

  • return (money, number, kind) triplets, and figure out what to update based on kind
  • or you can have a (data) class representing the State, and update that

See Python call mechanism

If turam returns a tuple anywhere, it should return a tuple everywhere. In particular, where you have return pieniadze, perhaps you should use return pieniadze, None, and let the caller ignore the second element of the received tuple if it does not care about it.

Alternatively, maybe turam should always return a single integer value, and it should use an internal recursive helper that returns a tuple. Something like

def turam(...):
    def helper(...):
        ...

   x, y = helper(...)
   return x
1 Like

yeah but i want to use both values

i dont want the code to return a tuple anywhere
i want to use both values seperately

yeah i know, but im asking for help to make the code not return a tuple, and both values are updated correctly and i can use them seperately

That’s how Python works: if your function returns multiple things, they’re packed into a tuple.

>>> def test():
...  return 1, 'a'
...
>>> type(test())
<class 'tuple'>

If you want to capture them as separate variables, it will do the unpacking for you, but you can run into trouble if there’s a mismatch in how many things it tries to unpack.

>>> something = test()
>>> something
(1, 'a')

>>> first, second = test()

>>> a, b, c = test()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: not enough values to unpack (expected 3, got 2)