We will code a game which challenges students to convert from decimal (denary, base 10) to hexadecimal, base 16.
Python has a cool little built-in function that we can use. You have already seen a few built-in functions.
There is another one called hex()
Open your IDE and type in the following code:
num = 10
hNum = hex(num) #or just hNum = hex(10)
print(hNum)
Can you see how it works?
Do you remember using:
random.randint(1,10)
This function gives us an INTEGER between 1 and 10 inclusive.
hex(num)
gives us a hexadecimal number in the form of a STRING.
Annoyingly, it gives us 2 unwanted characters at the start of the STRING.
Do you know how to get rid of those characters?
Here is a help video if you want a tip.
The user enters their name.
The game player will face 10 challenges.
Each challenge will present the user with a decimal number between 0-101
The player has to enter the correct hexadecimal value.
If the player is correct, (s)he gets a point.
At the end of the game:
where name is the name of the player.
Can you solve this problem using as many concepts covered in the course so far, including:
It might take a day, a week, a month or a year to solve it! Enjoy the experience!
Can you add your own ideas to the game.
For example, if the player gets an answer wrong, (s)he loses a point!
If the player end with negative points, a consequence or additional challenge is presented...
To help you plan your idea, design a main() function starting with comments eg:
def main():
#input name eg name = inputName() ... make name global
#set score to 0
#loop 10 times
#generate first challenge eg generateChallenge()
#prompt user to enter solution eg enterSolution()
Then you can start coding your solution using Python.