Python Python Collections (2016, retired 2019) Dungeon Game Dungeon Entrance

Setting Variables to None

Why are monster, door, and player initially set to None?

3 Answers

You can set a variable to None it python to initialize it, so that you can reference the variable later in the program. Hope this helps.

It is useful for defining variables "without actually giving them a value". If you need to define a variable but you don't need to set it to a value you can use None as the assignment value. In JavaScript it would look like this:

var moster;
var door;
var player;

In Python it would like this:

moster = None
door = None
player = None

This will create the variables, but they won't have a starting value.

Just keep learning Python and you'll see this is helpful in some cases.

I hope this helps. ~Alex

Thanks for the responses!

Can you give either of us (either Khaled or I) a Best Answer so that this question appears as answered in the Community? Thank you. ~Alex