THE HONG KONG POLYTECHNIC UNIVERSITY
ENG2002 Computer Programming Homework
- Instructions
- At the beginning of each Python file, add a comment to show your student ID and name.
- This assignment comprises 3 parts. You may use a new file for each part or a single Jupyter Notebook file.
- Your programs need to be well commented. You may refer to your course notes (ch4.pdf) on how comments aremade. Lacking comments will result in down-grading of your submission.
- Your programs need to have a good structure. Never write the whole program in a single function. You shouldwrite supporting functions.
- Do NOT share your source program with your classmates. Plagiarism will make you and your friends all get zermark.The deadline of submission is 13 October 2024 and you need to submit your file to Teams as an attachment.
- The Programming TaskBackground
A simple resistor–capacitor (RC) circuit is an electric circuit composed of one resistor and one capacitor as shown below
The capacitor C could be charged to and then be discharged by the resistor R. The formula for exponential decay ofis the capacitor voltage at time t = 0. The time required for the voltage to fall to 36.8% of the initial value is
called the time constant (?) and is given by:? = ??(2)Q1. Create a new file in Jupyter Notebook with the name "part1". On starting, the application should call a function
userMenu() that shows the following main menu:
[a] Find time constant
[b] Find discharge voltage
[c] Find discharge time
[d] Find best curve from experimental data
[x] Exit
Key in your choice (a, b, c, d or x to exit): • The user is expected to enter a character of 'a', 'b', 'c', 'd' or 'x' only. When any other character is entered,userMenu() will display "Wrong input, enter again!" and the above main menu is shown again. Forthis part, if the user enters 'b' or 'c' or 'd', the above menu will be shown again and your program does not needto do anything.
- If the user enters 'a', userMenu() will display "Please enter resistance (R): ". The user should input
a value of type float. It is assumed that the user always enters a positive float value for R and your programdoes not need to check this. Then, userMenu() will display "Please enter capacitance (C): ". Theuser should input 代 写ENG2002 Computer Programming Homework a positive value for C. If the user enters 0 or a negative value, userMenu() will prompt theuser for re-entering until a positive value has been entered (you may assume that the user always enters a valueof type float for this and your program does NOT need to check this.) Then, the function timeConst() iscalled, which uses the user-input values of R and C as input parameter values, and returns the value of the timeconstant, which is of type float. Note that this returned value should be printed by userMenu() and thefunction timeConst() must NOT print anything on screen.
- If the user enters 'x', userMenu() will print "Goodbye!" on the screen. The whole program then ends (withoutshowing the main menu.)
- The following Python code MUST be used.
#your Name
#student number
from math import exp, log #use math functions in part 2
#Implement the required functions below:
#def timeConst(R, C) #Find time constant of a simple RC circuit
#def userMenu() #shows the user menu and process user input
userMenu() #call function to display user menu and process user input
- The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.
Notice that the character(s) following a ‘:’ is/are entered by the user.
-------------------------------------- PASS line ----------------------------------Q2. Create a NEW file with the name "part2". Copy the content of the source file in Q1 to this file. Modify the
program developed for Q1 to perform the following tasks:
- Rewrite the function userMenu()such that if the user enters 'b', it will display "Enter initial voltage(V0): " on the screen. The user should input a positive value of type float and you should create a functionnput_p()to handle this. Then, userMenu()will display "Enter resistance (R): " on the screen. Theser should input a positive value of type float and the function input_p()should be used. Afterward,userMenu()will display "Enter capacitance (C): " on the screen. The user should input a positive valueof type float and the function input_p() should also be called. Then, userMenu()should call a functiondisVoltage() which uses the values of V0, R, C as input parameters. disVoltage()will display "Enterdischarge time (t): " on the screen. The user should input a positive value of type float and the functionnput_p()should be used. disVoltage()will then display the discharge voltage using equation (1). You mayuse exp()function in the math module. The function disVoltage() should have no returned value.
- Rewrite the function userMenu()such that if the user enters 'c', it will display "Enter initial voltage
(V0): " on the screen. The user should input a positive value of type float and the function input_p()should be used. Then, userMenu()will print "Enter resistance (R): " onthe screen. The user shouldinput a positive value of type float and the function input_p() should be used. Afterward, userMenu()willdisplay "Enter capacitance (C): " on the screen. The user should input a positive value of type floatnd the function input_p() should also be called. Then, userMenu()should call a function disTime() whichuses the values of V0, R, C as input parameters. disTime()will display "Enter discharge voltage (Vr):" on the screen. The user should input a positive value of type float and the function input_p()should beused. disTime()will then display the discharge time by solving equation (1). You may use log()function inthe math module. The function disTime() should have no returned value.
- The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.Notice that the character(s) following a ‘:’ is/are entered by the user.
---------------------------------- CREDIT line ------------------------------------
Q3. Create a NEW file with the name "part3". Copy the content of the source file in Q2 to this file. Modify theprogram developed for Q2 to perform the following tasks:
- Rewrite the function userMenu()such that if the user enters 'd', it will call the function bestCurve() and thenthe function will call input_p() to get the input data from the user. It will display the result on the screen
nside the function. Get back to the main menu after the result is shown.• To find the best curve, you need to transform equation (1) into straight line, y = m x + b andthen use the followingormulae to find the best straight line:
where
- The sample outputs given below should be realised by your program on executing it inside Jupyter Notebook.
Notice that the character(s) following ‘:’ is/are entered by the user.
---------------------------------- DISTINCTION line -------------------------------
标签:function,userMenu,Programming,should,will,Computer,user,input,Homework From: https://www.cnblogs.com/comp9313/p/18459730