0

I am trying to write a function that does the following:

If player_hand doesn't have any number that is less than or equal to mana_bank the function should do nothing.

If player_hand has a number that is less than or equal to mana_bank then the function should prompt the user with which number he chooses.

If the number the user chooses is less than mana_bank it should return a list with that number. If the number is more than mana_bank it should call the function again untill he prompts a number that is less than or equal to mana_bank.

Whenever I run the function and the user first prompts a number that is higher than mana_bank the function gets called from within itself, and then even if the user prompts a number lower than or equal to mana_bank, the function returns None instead of the number in a list.

I also tried making the variable player_choice global but it still didn't work.

Would appreciate any guidance.

def pick_card():
    player_choice_str = 0
    player_choice = []
    for i in player_hand:
        if i <= mana_bank:
            print(player_hand, mana_bank, "Mana available type the name of the card you wish to play ... ")
            player_choice_str = input()
            break
        if not i <= mana_bank:
            return "You don't have enough mana to play a card."
    if int(player_choice_str) > mana_bank:
        print("That card costs " + str(player_choice_str) + " and you only have " + str(mana_bank) + " this round.")
        pick_card()
    if int(player_choice_str) <= mana_bank:
        player_choice.append(player_choice_str)
        return player_choice



player_hand = [4, 8, 8]
mana_bank = 4

print(pick_card())
2
  • 1
    Just for your info, the user can crash your program with a RecursionError if giving a number greater than mana_bank too many times. Refer to this question for processing the user input properly. Commented Jun 2, 2020 at 15:41
  • If player has multiple cards <= mana_bank, should they be prompted for each card or just once overall? Commented Jun 2, 2020 at 15:56

2 Answers 2

1

Your first problem is that player_choice is defined locally. It will be overwritten in every function call. In addition, you have to evaluate/return the return value of pick_card() in the case when the players choice is greater than the manabank. Otherwise the first called function of the recursion will return None because of the missing return value.

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

Comments

1

In your for-loop you have both conditions - if i <= mana_bank: and if not i <= mana_bank: and for any given i one of these will be true.

So your loop will stop as soon as it hits a card that is greater than mana_bank and won't check all cards.

What you want to do is:

for i in player_hand:
    if i <= mana_bank:
        print(player_hand, mana_bank, "Mana available type the name of the card you wish to play ... ")
        player_choice_str = input()
        break
if player_choice_str is 0:             # no card selected at the end of the loop
    return "You don't have enough mana to play a card."

Also you will probably want to validate the user input after this line player_choice_str = input() to check they entered a valid card.

Comments

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.