The user needs to enter a four digit number or X or exit or similar to stop the game and essentially give up.
The spec says we have to make sure the user input has four digits and that each are different.
So we must do two checks on the number entered. I added one more check too to see if the input was numeric.
Check one is pretty easy does it have four digits - we can use the LEN command on the string to find this out. See strings. There are several pages about strings here.
This page has stepping through a string and shows LEN being used.
We might write some code that perhaps says
if Len(userinput) == 4:
This would find out if the user input has four characters.
To find out if there are any Duplicates in the input is a bit more tricky but we can use SETS to help up - a Set is a data structure that only allows one of each item to be put into it.
if we made the userinput into a set - inputSet = set(usetinput) then we can check the length of that set - if it is not 4 then there must have been a duplicate in the input.
We can use another feature to find out if the user input is made up of numbers we can use isdigit()
if userinput.isdigit() == True :
We could put that together into a def that would check anything sent to it - we often call this checking validation. Send the user input to the routine as a parameter and return true or false -
Try this practice program - below to see the effect of using lengths and sets.
Below is my actual program running showing the validation working
When I run my code if I put too many digits in it says it is invalid.
When I run my code if I put two duplicates in it says it is invalid
If I run my code and enter letters it says it is invalid.