0

I can't seem to get this working. I've got a loop for a simple card game of higher or lower, and what I'd like to do, is return the card or any values of the card, and the points so that I can pickle them for storage and play this game over and over. But, I can only return the card and points in the first iteration of the loop. Whenever the gameloop function is called within the function again, the print function on the very last line prints None.

def gameloop(rand_card, rand_type, point):
    point = point
    card = rand_card, rand_type
    print("The card is:")
    print_card(card[0], card[1])
    print("Do you bet on a higher or lower value to be drawn? (H/L)")
    ans = input().lower()

    if ans == "q":
        return card, point

    new_card = random_card(), random_type()
    print("The new card is:")
    print_card(new_card[0], new_card[1])


    if ans == "h":
        if new_card[0] > card[0]:
            print("Congratulations, the new card was higher. You have been awarded one point.")
            point += 1
        elif new_card[0] < card[0]:
            print("Sorry, the new card was lower. One point has been deducted.")
            point -= 1
        else:
            print("It's a tie! No points deducted.")

    elif ans == "l":
        if new_card[0] < card[0]:
            print("Congratulations, the new card was lower. You have been awarded one point.")
            point += 1
        elif new_card[0] > card[0]:
            print("Sorry, the new card was higher. One point has been deducted.")
            point -= 1
        else:
            print("It's a tie! No points deducted.")

    print("You have a total of {} points.".format(point))
    gameloop(new_card[0], new_card[1], point)

data = gameloop(random_card(), random_type(), 0)

print(data)
  • gameloop function doesn't `return` anything. Therefore returns None – William Baker Morrison Jan 07 '21 at 10:40
  • 1
    gameloop function does have return card, point But it still returns None, after one iteration. With only one iteration it returns the card and point, but thats really not helpful in the first iteration. – MHB_2001 Jan 07 '21 at 10:41
  • Did you mean: `return gameloop(new_card[0], new_card[1], point)`? – quamrana Jan 07 '21 at 10:41
  • I'm still new, totally new, but I just replaced that with the old return statement, and that just keeps me trapped in a loop. – MHB_2001 Jan 07 '21 at 10:48
  • if you exit immediately ( press q as your first choice ) you can see the returning values. but if you press H or L at least once before q, it returns nothing (None) and the value of data becomes None. To make it simpler avoid using recursive call ( calling a function inside itself) and loop through gameloop() outside the function. – parvaneh shayegh Jan 07 '21 at 10:53
  • That would totally work as well, I have found that quamrana was totally right as well. – MHB_2001 Jan 07 '21 at 10:57

1 Answers1

0

return gameloop(new_card[0], new_card[1], point) instead of gameloop(new_card[0], new_card[1], point), solves the problem, thank you quamrana.