Your task is to implement a simple calculator script ask the user
to enter a number, then an operation (like + and /), then a
number again. After the 2nd number input, the result of the
operation should be calculated and printed out. Then the program
should start asking again for a first number.
The script should exit when the user enters a non-numeric expression (like a letter) for the first number input.
!> This is a guided project, a regular project with a step-by-step guide (see the Background materials). In order to learn the most, try and implement it on your own first, and check the solution only when you feel your version is ready. However, when you feel completely stuck, feel free to check the guide for hints.
- more advanced conditional statements
- type conversion
- basic exception handling
-
Implement
is_number(str)to return the numeric value of the stringstrif possible, otherwise returnNone- The checker works for strings representing non-negative integers.
- The checker works for strings representing any integers.
- The checker works for strings representing any numbers.
- The checker returns either a number or
None, and never raises an exception
-
Implement
is_valid_operator(operator)to return withTrueif theoperatorinput parameter represents a valid operator, otherwise return withFalse.- Valid operators are the following: +, -, *, /.
-
Implement
ask_for_a_number(force_valid_input)to return the numeric value of the user input. The function continuously ask an input from the user while it is not numeric whenforce_valid_inputisTrue. When theforce_valid_inputisFalseand the user input is not numeric, the function returns withNone.- The function asks an input from the user, e.g.: 'Please provide a number! '.
- The function returns with the numeric value of the input string if it represents a valid number.
- The function returns with
Nonewhen the user input not represents any number andforce_valid_inputisFalse. - In case of
force_valid_inputisTrue, the function continuously ask for a number, while the provided input string not represents a valid number. After an unsuccessful input, the program inform the user about the wrong input, e.g.: 'This didn't look like a number, try again.'.
-
Implement
ask_for_an_operator(force_valid_input)to return with a valid operator. The function continuously ask an operator from the user while it is not valid operator whenforce_valid_inputisTrue. When theforce_valid_inputisFalseand the user input is not a valid operator, the function returns withNone.- The function asks an input from the user, e.g.: 'Please provide an operator (one of +, -, *, /)! '.
- The function returns with an operator if the input string represents a valid operator.
- The function returns with
Nonewhen the user input not represents valid operator andforce_valid_inputisFalse. - In case of
force_valid_inputisTrue, the function continuously ask for an operator, while the provided input string not represents a valid operator. After an unsuccessful input, the program inform the user about the wrong input, e.g.: 'Unknown operator.'.
-
Implement
calc(operator, a, b)to calculate the result of the 'a' 'operator' 'b' operator. The function returns withNoneif any operand or the operator is not valid. The function handles division by zero, in this case the return value isNone- The function checks the validity of the operands and the operator. Returns with
Nonein case of any invalid input. - The function returns with the result of 'a' + 'b' if the operator is '+'.
- The function returns with the result of 'a' - 'b' if the operator is '-'.
- The function returns with the result of 'a' * 'b' if the operator is '*'.
- The function returns with the result of 'a' / 'b' if the operator is '/' and b is not equal to 0. If the 'b' is equal to 0, the function prints an error message, e.g.: 'Error: Division by zero' and returns with
None.
- The function checks the validity of the operands and the operator. Returns with
-
Implement
simple_calculator()to create the calculator. The function continuously asks for number 'a' and 'b' and an operator and calculates the result of 'a' 'operator' 'b'.- The function checks the validity of the operands and the operator. Exit from the program if the 'a' operand is not valid. In case of valid 'a' operand, the function ensures the operand 'b' and the operator is valid.
- The function prints the calculation results, e.g.: 'The result is 10'.
None
- You need a function for asking input number from the user. For the first number request, non-number input is acceptable (this will trigger the exit from the program), but for the second input request is not allowed. You can distinguishes between the two case with a boolean variable.
- Create a helper function called input, which will contain the Scanner. It is a good idea to outsource all functionality that is used more than once.