Lab 2
Python User Input
Objective
1) เพื่อเข้าใจการใช้ตัวแปร Variables และการกำหนดค่าให้แก่ตัวแปร
2) เพื่อให้เข้าใจการคำนวณด้วยสมการ
3)เพื่อให้เข้าใจการแสดงผลหน้าจอคอมพิวเตอร์
Python allows for user input via the keyboard. That means we are able to ask the user for input. Python stops executing when it comes to the input() function, and continues when the user has given some input.
Example
In the example above, the user had to input their name on a new line. The Python input() function has a prompt parameter, which acts as a message you can put in front of the user input, on the same line:
The input from the user is treated as a string. Even if, in the example above, you can input a number, the Python interpreter will still treat it as a string. You can convert the input into a number with the int() or float() function.
or get the input and convert in a same time
Example from Lab1
To get Number from user into Variables in a program
print("*********************************************")
print("* Calculation of Serie Circuit Program *")
print("*********************************************")
E = int(input("Please Enter number of E:"))
R1= int(input("Please Enter number of R1:"))
R2= int(input("Please Enter number of R2:"))
R3= int(input("Please Enter number of R3:"))
Rt =R1+R2+R3
it =E/Rt
V1 =R1*it
V2 =R2*it
V3 =R3*it
P1 =it*it*R1
P2 =it*it*R2
P3 =it*it*R3
Pt = P1+P2+P3
print("การคำนวณหาความต้านทานรวม Rt =", end="")
print(Rt,end="")
print("โอห์ม")
print("V1 =", end="")
print('%.2f' %(V1),end="")
print(" Volts")
print("V2 =", end="")
print('%.2f' %(V2),end="")
print(" Volts")
print("V3 =", end="")
print('%.2f' %(V3),end="")
print(" Volts")
print("it =", end="")
print('%.2f' %(it),end="")
print(" Amps")
print("P1 =", end="")
print('%.2f' %(P1),end="")
print(" Watts")
print("P2 =", end="")
print('%.2f' %(P2),end="")
print(" Watts")
print("P3 =", end="")
print('%.2f' %(P3),end="")
print(" Watts")
print("Pt =", end="")
print('%.2f' %(Pt),end="")
print(" Watts")
Result of Program
-------------------------------------------------------------------------------------------
assignment
Write code to to ask the user for input (E,R1,R2) and calculate a DC parallel circuit, find the specified values, and display the results on the screen as shown in the example image.