skeleton program notes Flashcards
in the displayscore() function, why is the score converted into a string
to allow string concatenation (string + string) because string+integer will give u an error
why is it necessary to validate the user input in CheckIfUserInputValid() function
- ensures that only formatted mathematical expressions are processed
- without validation, the program might try to parse invalid inputs
- this may raise runtime errors such as syntax errors
what does end=“” do
ensure that the output from the print() function doesn’t move to a new line after each fall
- instead, everything printed remains on the same line
what does the CheckValidNumber() function do
is responsible for validating whether a number (in string form) is a valid number to be used in the game, based on certain criteria
how the CheckValidNumber() function works
- Regex Check:
- uses the re.search() function to check if the Item string consists of only digits (^[0-9]+$). This regular expression ensures that the string only contains numeric characters. If it matches, the function proceeds; otherwise, it returns False. - Convert to Integer:
After confirming that Item is a valid number (made up of digits only), the function converts Item to an integer using int(Item) and stores it in ItemAsInteger.
Range Check:
- The function then checks if the integer is within a valid range (i.e., greater than 0 and less than or equal to MaxNumber). If so, it returns True, indicating that the number is valid.
If any of these checks fail, the function returns False, indicating that the number is not valid.
what is the purpose of the RemoveNumbersUsed() function
The RemoveNumbersUsed() function is responsible for updating the list of available numbers (NumbersAllowed) after the user has used some of the numbers in a mathematical expression.
- This function ensures that once a number has been used, it is removed from the list and cannot be used again until the list is refreshed
step by step analysis of how RemoveNumbersUsed() works
Convert User Input to Reverse Polish Notation (RPN):
The function begins by calling ConvertToRPN() on the UserInput. This converts the mathematical expression into RPN format, which is easier to evaluate programmatically.
Iterate Through the RPN Expression:
- The function then loops through each item in the RPN expression (UserInputInRPN), which is a list of operands and operators.
Check if the Item is a Valid Number:
- Inside the loop, CheckValidNumber() is used to verify whether the item in the RPN expression is a valid number within the allowed range (i.e., it should be a number and fall within MaxNumber).
Remove Used Numbers:
- If the item is a valid number, it checks if that number exists in the NumbersAllowed list. If it does, it removes the number from the list, indicating that the number has been used.
Return the Updated List:
- Once all valid numbers used in the expression have been removed, the function returns the updated list of available numbers
Function: CheckNumbersUsedAreAllInNumbersAllowed() purpose
- is responsible for checking whether the numbers in the user input expression (converted into Reverse Polish Notation) are valid.
- Specifically, it ensures that the numbers used in the expression are all available in the NumbersAllowed list, which is crucial for game integrity.
Function: CheckNumbersUsedAreAllInNumbersAllowed() how it works
takes two inputs:
- NumbersAllowed: A list of numbers that the user can use in their calculations.
- UserInputInRPN: A list of operands and operators in Reverse Polish Notation (RPN).
- The function checks if every number in the UserInputInRPN list (which is a converted version of the user’s mathematical input) is in the NumbersAllowed list. If all numbers are valid, it returns True; if any number is invalid, it returns False.
Function: CheckNumbersUsedAreAllInNumbersAllowed() annotations
Create Temporary List (Temp):
- We first create a copy of the NumbersAllowed list by iterating over it and appending each item to the Temp list.
- This is done to avoid modifying NumbersAllowed directly while iterating.
Loop through UserInputInRPN:
- The UserInputInRPN list contains all the operands (numbers) and operators from the user’s input expression (after converting it to RPN). We loop through this list to check each item.
Check if the Item is Valid:
- CheckValidNumber(Item, MaxNumber) checks if the item is a valid number.
- This ensures the user input consists only of valid, allowed numbers. The validation checks if the number is within the acceptable range (from 1 to MaxNumber).
Check if the Item Exists in Temp:
- If the number is valid, we then check if the number exists in Temp. If it does, we remove it from Temp, ensuring that each number can only be used once.
Return True or False:
- If any number is not found in Temp, we return False, indicating that the user has attempted to use an invalid number (i.e., a number not allowed or already used).
If we finish checking all the items without finding an invalid one, we return True.
What would happen if the numbers in UserInputInRPN are not removed from Temp after being used?
- CheckNumbersUsedAreAllInNumbersAllowed()
If the numbers are not removed from Temp, the function would still consider them available for subsequent uses, meaning the user could reuse the same number multiple times even if they have already used it. This could lead to an invalid game state.
CheckIfUserInputEvaluationIsATarget Function() purpose
checks whether the evaluated user input matches any of the target numbers in the game.
- If the input matches a target, score’s updated and target from the list is removed - rewards the user with points if they get it right.
CheckIfUserInputEvaluationIsATarget Function() how it works
- use the EvaluateRPN function to evaluate the user’s input, which has already been converted into (RPN). This will return the result of the mathematical expression.
- If the expression is invalid (for example, division by zero or an empty input), EvaluateRPN returns -1. - Check for a Valid Evaluation: If the result of the user’s expression (UserInputEvaluation) is not -1, the function proceeds to check whether the evaluation matches any of the targets.
- Loop Through Targets: The function loops through the list of targets. If any target matches the result of the user input evaluation, it
- Adds 2 points to the user’s score.
- Sets that target to -1 to indicate it’s been “used” or “hit”.
- Sets a flag (UserInputEvaluationIsATarget) to True to indicate the user hit a target. - Return Values: The function returns two values:
- UserInputEvaluationIsATarget: A boolean indicating whether the user hit a target.
- Score: The updated score, including the points for hitting a target.
for Count in range(0, len(Targets)): what does this mean
- iterates using the index (Count) of each element in the list.
- eg if the target list has 10 elements, the iteration would be Target[0] , Target[1], Target[2] ,….Target[9] , (10 not included)
Why is the variable UserInputEvaluationIsATarget necessary?
to indicate if the user hit a target. (set to True is this happens)
What does EvaluateRPN(UserInputInRPN) do?
Evaluates the user’s input expression in Reverse Polish Notation (RPN) format. This converts the user input (parsed into RPN) into its numerical result (a single number or -1 for invalid inputs).