Tails and Heads Game

Good day to everybody! Can somebody help me with the code ?
I get this error message : Hey, welcome to Heads and Tails game!
Traceback (most recent call last):
File “script.py”, line 21, in
HeadsTails(Tail, 11)
NameError: name ‘Tail’ is not defined

Here is my Code


The Traceback is telling you where your error is, and what is wrong:

Traceback (most recent call last):
File “script.py”, line 21, in
HeadsTails(Tail, 11)                  <<< this line is the problematic one
NameError: name ‘Tail’ is not defined <<< and this line is explaining the problem

When Python says name 'Tail' is not defined, it is saying “I don’t know what that variable is”. This is because you don’t have a global variable in that code snippet called Tail.

You’ve defined that variable inside your function HeadsTails, which makes it locally-scoped variable. It exists only within that function, and is only accessible to other code within that function. You can’t access it from outside.

Additionally, there are some other mistakes you’ve made with the program. For example, your HeadsTails function accepts a parameter guess… but almost immediately you’re re-assigning this, inside the function, by asking the user to input a new guess.

Same thing with bet - it’s a parameter in your function, but again you’re re-assigning it inside of the function by taking user input.

Hopefully that’ll help you solve the immediate error, and improve the overall structure of your coin flipping program. :slight_smile:

1 Like

But how do you ask the player for the guess and bet if you dont ask him the question and dont assign the answer as bet and guess ?

The point I was trying to make is this:

Your function, as it is defined, will error if you don’t provide the two parameters - guess and bet.

There’s no point in these being parameters if you’re just going to reassign them immediately in your function.


  • you ought to be asking for the guess and bet amount outside of the function and passing them in as parameters,
  • you ought to remove the parameters since you’re not using them, and just ask for the guess/bet inside of the function.

Doing both in this case is redundant.