Title: Coin Flip Simulator - Python
Tags: python
Question: I started learning python (it is also the first programming language I am learning) about 6 weeks ago, and I am working on various small projects for practice.

On this one, I am trying to build a coin flip simulator that will keep asking the player to toss the coin until they say no and returns the results in a dictionary, see code below

The problem with my code is that when I type "no" to break out of the while loop it calls coin_flip() once more, and I do not understand why. E.g. if the player plays 4 times, the program tosses the coin 5 times.

Also, I am using this project as a means to practice while loops, so if you could troubleshoot along those lines, I would appreciate it greatly.

Thanks in advance!


    import random

```def coin_flip():
    if random.randint(0, 1) == 1:
        return 'Head'
    else:
        return 'Tails'


def coin_flip_simulator():
    coin_dict = {'Head': 0, 'Tails': 0}
    ask_play = input('Would you like to flip a die? Enter Yes or No: ').upper()
    if ask_play[0] == 'Y':
        flip = coin_flip()
        coin_dict[flip] += 1
        print(flip, coin_dict)
        while ask_play[0] != 'N':
            flip = coin_flip()
            coin_dict[flip] += 1
            ask_play = input('Would you like to flip again? ').upper()
            print(flip, coin_dict)

        print(f'Thank you for playing! Your total score is {coin_dict}')
    else:
        print('No worries!')


coin_flip_simulator()
```



Comment: You can use just one `while` loop with no `if` outside.


Here is another answer: You are calling coin_flip() again after the user says "N". You can remove that duplicate code, something like this: 

```import random


def coin_flip():
    if random.randint(0, 1) == 1:
        return 'Head'
    else:
        return 'Tails'


def coin_flip_simulator():
    coin_dict = {'Head': 0, 'Tails': 0}
    ask_play = input('Would you like to flip a die? Enter Yes or No: ').upper()
    #This will only run as long as the user keeps saying no
    while ask_play[0] != 'N':
        flip = coin_flip()
        coin_dict[flip] += 1
        print(flip, coin_dict)
        ask_play = input('Would you like to flip again? ').upper()
    print(f'Thank you for playing! Your total score is {coin_dict}')
    print('No worries!')


coin_flip_simulator()
```

Comment for this answer: Use `input()` for python 3. OP is using python 3.

Here is another answer: Glad to hear you are starting in the amazing world of Python. I put together some code based on what you were trying to achieve. Please give this code a try and let me know if it works.



```import random

def coin_flip():
    if random.randint(0, 1) == 1:
        return 'Head'
    else:
        return 'Tails'


def coin_flip_simulator():
    coin_dict = {'Head': 0, 'Tails': 0}
    player_name = input('What is your name? \n ')
    ask_player = input(f'{player_name}, Would you like to flip a coin? (Y/N): ').upper()
    while ask_player != 'N':
        flip = coin_flip()
        coin_dict[flip] += 1
        print(f"Current Score is: ==&gt; {coin_dict}")
        ask_player = input(f'Would you like to flip again? ').upper()

    print(f'\nNo worries,{player_name}!, thank you so much for playing! \nYour total score was =&gt; {coin_dict}')

coin_flip_simulator()
```


Here is another answer: An option that should do the job below, preserving the behavior in messages you want (I think)

```import random


def coin_flip():
    if random.randint(0, 1) == 1:
        return 'Head'
    else:
        return 'Tails'


def coin_flip_simulator():
    coin_dict = {'Head': 0, 'Tails': 0}
    ask_play = input('Would you like to flip a die? Enter Yes or No: ').upper()
    if ask_play[0] == 'Y':
        while ask_play[0] != 'N':
            flip = coin_flip()
            coin_dict[flip] += 1
            print(flip, coin_dict)
            ask_play = input('Would you like to flip again? ').upper()

        print(f'Thank you for playing! Your total score is {coin_dict}')
    else:
        print('No worries!')


coin_flip_simulator()
```

You played twice the first time. Just removed some lines and it makes it

Comment for this answer: @Daphoque I granted the code of the minimal corrections from original to make it working. That's how you see what is wrong. Rewriting the stuff does not help. And elegance has no place in learning!
Comment for this answer: I see you never faced any student or people to seriously teach to, my dear friend. :)
Comment for this answer: Your if "if ask_play[0] == 'Y':" is useless, the answer is checked with the while below.
Comment for this answer: Indeed, but efficiency yes :) We're not asking you to rewrite the stuff, but you can help to improve the efficiency of the code when you see something wrong, and help people to code in a better way :D

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/25/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python>', 'Title': 'Coin Flip Simulator - Python', 'ViewCount': 257, 'id': 61995043, 'user_AboutMe': None, 'user_AccountId': 18672000.0, 'user_DisplayName': 'Periklis06', 'user_Reputation': 9.0, 'user_Views': 4.0, 'user_id': 13610525.0}}