PCAP 31 03 PCAP Certified Associate Python Programmer · Free Practice Question Easy
Question 1
Q349 - Data Types
You are an intern for ABC electric cars company.
You must create a function that calculates the average velocity
of their vehicles on a 1320 foot (1/4 mile) track.
The user needs to be able to insert floats.
Consider the following code.
- distance = ???(input('Enter the distance travelled in feet'))
- distance_miles = distance/5280 # convert to miles
- time = ???(input('Enter the time elapsed in seconds'))
- time_hours = time/3600 # convert to hours
- velocity = distance_miles/time_hours
- print('The average Velocity : ', velocity, 'miles/hour')
The output must be as precise as possible.
What would you insert instead of ??? and ???
-
A
- int
- int
-
B
- int
- float
-
C
- float
- int
-
D
- float
- float
Reveal correct answer
Correct answer: D
Explanation
Topics: input() float() int() division operator
Try it yourself:
- # distance = float(input('Enter the distance travelled in feet'))
- distance = float('437723.42') # Just for convenience
- distance_miles = distance/5280
- # time = float(input('Enter the time elapsed in seconds'))
- time = float('1723.9') # Just for convenience
- time_hours = time/3600 # convert to hours
- velocity = distance_miles/time_hours
- print('The average Velocity : ', velocity, 'miles/hour')
- # The average Velocity : 173.1236071486956 miles/hour
- print((float('437723.42')/5280) / (float('1723.9')/3600))
- # 173.1236071486956
- # print((int('437723.42')/5280) / (int('1723.9')/3600))
- # ValueError
- print((int(float('437723.42'))/5280) / (int(float('1723.9'))/3600))
- # 173.21387115496228
Explanation:
To be as precise as possible means,
that you do not want to loose the numbers after the decimal point,
if they are given by the user.
Therefore you need float() for both inputs.
Assuming the user enters a float
the function int() would raise a ValueError anyway.
Q349 (Please refer to this number, if you want to write me about this question.)
A. Using 'int' for both distance and time inputs would truncate the decimal parts, leading to inaccurate calculations for both distance travelled and time elapsed. This would result in an incorrect average velocity calculation.
B. Using 'int' for the distance input would truncate the decimal part, leading to inaccurate calculations of distance travelled. Using 'float' for the time input is correct because time can be represented as decimal values.
C. Using 'float' for the distance input is correct because distances can be represented as decimal values. However, using 'int' for the time input would truncate the decimal part, leading to inaccurate calculations of time elapsed.
D. The 'float' function is used to convert the input value to a floating-point number, which is necessary for accurate calculations involving decimal values like distance and time in this context.
Discussion
Think the marked answer is wrong, or have a better explanation? Share it below — comments appear after review.
