Python Project
ISE330 - Engineering EconomyFall ‘95 ET2100 – Programming in PythonFall 2020-2021 ET2100Fall 2020-2021 Programming project 1 - Instructions Task Title: Implementation of basic command-line interface Task Instructions: For project 1, you are asked to design and implement a program that has a menu of mathematical operations. The user should select an option from the menu of the operation needed (input), check if that option is a valid input, then the program should ask again for an argument input, check its validity then perform the required operation based on what is given above. First, we need to understand the problem and devise a plan on how to solve it, ie. design our program. Then, we will define steps for solving and solve each step while testing the program along the way. From problem description we have the following steps in the program execution: 1. Prompt the user for operation choice 2. Take input and validate it and echo it back 3. Prompt user for numerical argument for the math operation 4. Take input 5. Perform appropriate math operation 6. Store the operation and argument into history 7. Print the result 8. Loop to ask user for the next operation 9. Extend implementation for other user choices And then repeat those steps until user enters quit or exit. Step 1 and 2 operation choice (and 8) So for the implementation you should start from the loop (we learned in week 5 loops with user input. One possible implementation might be: while True: dns_input = input(dns_prompt) print (f'User input is {dns_input}') Test it now (save file and execute your program). Fix any errors (there is one - you need to define the prompt variable and you can use the text from the project statement directly), also it may make an infinite loop. We need to test for end of the program inside the loop: if dns_input.lower() == 'quit' or dns_input.lower() == 'exit': break Enter this code inside the loop after print statement. Test again (with EXIT option). Step 3 and 4 Argument choice After the user selects the operation he/she/they need to enter argument and them program should prompt them. Also, argument should be converted to a number (keep in mind that this text needs to be indented): dns_num_text = input("\nEnter the argument for operation -> ") dns_number = float(dns_num_text) Test it to verify that it works (you may print the argument). Step 5 and 7 Calculation Once the program gets dns_input and dns_number we can perform the math calculations, though actual math function depends on the user input, so we need to use if, for example: if dns_input == ‘1’: res = math.sin(dns_number) print(f'sin({dns_number})={res}') As you see, we also print our result using f-string. Test this result, for example using sin(90), it should be 1. Is it correct? If not think about degrees and radians, find a math function for the conversion. After corrected the code test it again, did you get sin(90)=1 now? If so you are good!! Do not forget to import math library at the top of the file. Step 6 History For history, there are several ways to approach it. You may store history as 2 lists, operations and arguments, or as dictionary hist_dict with operation as key and argument as value. Let us assume that we use two lists. Both lists have to be initialized at the top of the program (before the loop) operations = [] arguments = [] and after the calculation we use append method to add elements to the lists (again keep indentation in mind): operation.append(dns_input) arguments.append(dns_number) Now test it few times for several sin operations, you can see the content of the list after you end the program. Now is good time to implement choice 10 (History). What do we need to say? if dns_input == ‘10’: # print history To print the history you should use for loop and print the operation and its argument on the same line, with one pair per line. See your week 5 labs and homework to find out how to do that. Test program again, and check that history is printed correctly. Fix any syntax and/or logical errors. Step 9 Repeat for other operations Once you have your program working with only one operation allowed, you should extend it to include other operations. For example, cos operation is almost identical to sin, you just need to replace math function. On the other side, some functions require additional tests, like ln and sqrt requiring positive number, and factorial requiring integer. As you add those options you need to test them right after implementation, do not wait to test at the end. The program can be improved to validate user inputs further, for example gracefully reject any string that is not a number, notify the user, and give the user chance to enter value again. 1 1 2