-2

I'm currently trying to create sort of a poker game were you input an int (how many rounds to play) then have both Human and Computer generate cards based on the int you put and decide who wins for each round. My question is how do I return an argument from a function into another function? I have the x, y in function poker() that I want to use to compare in function totalwins().

import random

Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1,2,3,4,5,6,7,8,9,10,11,12]


def poker(x,y):
    Times = int(input('How many rounds would you like to play: '))
    
    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)
        
    
        print('Human: ', Human , Human_value)
        print('Computer: ', Computer, Computer_value)
        
        if Human_value > Computer_value:
            print('Human wins')
            x +=1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1
poker(0,0)


def totalwins():
    
    if x > y:
        print('Human wins!')
    else:
        print('Computer wins!')
totalwins()
null_override
  • 467
  • 3
  • 10
  • 30
Jarckil
  • 7
  • 2
  • One way of doing is to create a `class`, so these parameters can be accessed by all instance methods using `self` – Sociopath Sep 14 '20 at 06:22
  • Does this answer your question? [How is returning the output of a function different from printing it?](https://stackoverflow.com/questions/750136/how-is-returning-the-output-of-a-function-different-from-printing-it) – SiHa Sep 14 '20 at 06:40

3 Answers3

0

The code is messy, but here is how to pass a return value to next function


Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]


def poker():

    # instead of passing x, y as 0 i am initializing inside
    x = 0
    y = 0

    Times = int(input('How many rounds would you like to play: '))

    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)

        print('Human: ', Human, Human_value)
        print('Computer: ', Computer, Computer_value)

        if Human_value > Computer_value:
            print('Human wins')
            x += 1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1

        # returning x, y to the next funcction
        return x, y


def totalwins():
    x, y = poker()
    if x > y:
        print('Human wins!')
    else:
        print('Computer wins!')


totalwins()
Anmol Parida
  • 672
  • 5
  • 16
  • Thank you, it worked. i did not know that i could return it like that. (Btw had to delete the indent for the return statement and get it outside the loop otherwise it would only play once) – Jarckil Sep 14 '20 at 06:34
  • yeah pasting issue, did not format after putting the comment over return statement. Please accept the answer @Jarckil – Anmol Parida Sep 14 '20 at 06:38
0

Perhaps a return statement at the end of poker? Something like:

return (x,y)

Then catch the values when you call poker:

x,y = poker(0,0)

And then change the argument list of totalwins to accept x and y and call:

totalwins(x,y)

Or you could make x and y global, but I wouldn't go that route

NNN
  • 697
  • 1
  • 5
  • 15
0

You call the function total_wins(x,y) once the while loop is over inside poker function.

As the variables x and y are incremented inside the poker function, the function total_wins should be called inside it as shown in the corrected code below:

import random
Deck = ['Diamond', 'Hearts', 'Jack', 'Spade']
Number = [1,2,3,4,5,6,7,8,9,10,11,12]

def totalwins(x,y):
    if x == y:
      print('It\'s a tie!')
    elif x > y:
        print('Human wins!')
    else:
        print('Computer wins!')

def poker(x,y):
    Times = int(input('How many rounds would you like to play: '))
    
    while Times > 0:
        Human = random.choice(Deck)
        Human_value = random.choice(Number)
        Computer = random.choice(Deck)
        Computer_value = random.choice(Number)
    
        print('Human: ', Human , Human_value)
        print('Computer: ', Computer, Computer_value)
        
        if Human_value > Computer_value:
            print('Human wins')
            x +=1
        elif Human_value < Computer_value:
            print('Computer wins')
            y += 1
        else:
            print('Tie')

        Times -= 1
    totalwins(x,y)
poker(0,0)
  • PS: Also have added a condition of Tie inside the total_wins function.
  • Note: Make sure to place the total_wins function before the poker function.
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Thanks alot! this actually really helped me understand how to use functions better. i did not know until now that i could call a function within a function and use its own arguments. – Jarckil Sep 14 '20 at 06:51