Complete Python Series · Part 14 of 29
Assignment 01-Price Converter
NextGen CTO — Python Assignments
A collection of practical Python exercises based on real-world scenarios from the NextGen CTO learning platform. Each assignment includes starter code, hints, a complete solution, and an explanation.
Exercise: NextGen CTO Course Price Converter
Build a simple currency conversion feature for the NextGen CTO learning platform. Learn to capture user input, perform arithmetic calculations using variables, and display formatted currency values using f-strings.
Problem statement
NextGen CTO offers programming courses to students across the world. While course prices are listed in Indian Rupees (INR), international students may want to view prices in US Dollars (USD).
You are building a simple currency conversion feature that accepts a course price in INR, converts it into USD using a fixed exchange rate, and displays the converted price with exactly two decimal places.
Task requirements
- Define a variable for the fixed exchange rate: 1 USD = 85 INR.
- Capture the course price in INR using
input()and convert it to afloat. - Calculate the equivalent course price in USD.
- Print a message using an f-string that says:
NextGen CTO course price: X INR = Y USD. - Display the USD amount with exactly two decimal places.
Constraints
- Use standard arithmetic operators.
- Do not use
ifstatements or loops. - Ensure the input is treated as a number, not a string.
- Use a fixed exchange rate of 85 INR per USD for this exercise.
Good luck trying the exercise!
Starter code
# TODO: Define the exchange rate (1 USD = 85 INR)
# TODO: Get the course price in INR and cast it to a float
# TODO: Calculate the equivalent course price in USD
# TODO: Print the result using an f-string with 2 decimal precision
Expected output
Enter NextGen CTO course price in INR: 4999
NextGen CTO course price: 4999.0 INR = 58.81 USD
Hints
- Remember that
input()always returns a string. Usefloat()to convert it into a number. - To convert INR to USD, divide the INR amount by the exchange rate.
- Division uses the
/operator. - Use the format specifier
:.2finside an f-string to display exactly two decimal places.
Solution: NextGen CTO Course Price Converter
Explore how to implement a currency conversion feature for an online learning platform using variables, user input, type casting, arithmetic operators, and formatted strings.
The solution uses float() to handle decimal course prices and f-string formatting to display the converted price with consistent precision.
# Define the exchange rate: 1 USD = 85 INR
exchange_rate = 85
# Get the course price in INR and cast it to a float
course_price_inr = float(input("Enter NextGen CTO course price in INR: "))
# Calculate the equivalent course price in USD
course_price_usd = course_price_inr / exchange_rate
# Display the converted course price
print(f"NextGen CTO course price: {course_price_inr} INR = {course_price_usd:.2f} USD")
Solution explanation
- Line 2: We store the fixed exchange rate of
85in a descriptive variable namedexchange_rate. - Line 5: We combine
input()andfloat()to capture the course price and convert it into a floating-point number. - Line 8: We calculate the equivalent course price in USD using the division operator
/. - Line 11: We use an f-string to display the original and converted course prices. The format specifier
:.2fensures that the USD amount is displayed with exactly two decimal places.
For example, a calculated price of 50.0 will be displayed as 50.00, making the output consistent with professional pricing displays on e-commerce and online learning platforms.