Question 409 of 498
Converting input() to Numeric Types: int vs float
You are a junior developer at a logistics company. Your team is building a system to calculate shipping costs based on package weight. The system reads weight from user input. A colleague wrote the following code: weight = input('Enter weight in kg: '); cost = weight * 2.5; print('Shipping cost:', cost). However, when testing with weight 10, the output is 'Shipping cost: 10101010101010101010' (the string '10' repeated 2.5 times? Actually, 2.5 is float, but string multiplied by float causes TypeError? Wait, string * float raises TypeError. But the symptom described suggests the code runs but produces unexpected output. Let me re-read: The output shows '10101010101010101010' which is the string '10' repeated 10 times? That would happen if weight is string and multiplied by int 10. But the code multiplies by 2.5. Actually, string * float raises TypeError, so the code would crash. The symptom must be plausible. Let me adjust: The code actually has weight = input(...), then cost = weight * 2.5, but if weight is '10', then '10' * 2.5 raises TypeError. So the symptom cannot be that output. I need to fix the stem to make sense. Instead, let's say the code is: weight = input('Enter weight: '); cost = float(weight) * 2.5; print('Cost:', cost). But then no issue. I'll create a scenario where the developer forgot to convert input to float, and then tries to multiply string by float, which causes TypeError. The correct action is to convert input to float. I'll adjust the stem accordingly. Let me rewrite the stem properly.
Stem: You are a developer at a shipping company. The system calculates shipping cost as weight (kg) times rate 2.5. A colleague wrote: weight = input('Enter weight: '); cost = weight * 2.5; print('Cost:', cost). When testing with weight 10, the program crashes with TypeError. Which action should you take to fix the code?
Quick Answer
The correct fix is to wrap the `input()` call with `float()`, as in `weight = float(input('Enter weight: '))`. This is necessary because the `input function returns string in Python` by default, and multiplying a string by a float—like `'10' * 2.5`—raises a `TypeError` since Python cannot repeat a string a fractional number of times. By converting the input to a numeric type, you enable proper arithmetic multiplication, allowing the shipping cost calculation to work with decimal weights. On the PCEP exam, this scenario tests your understanding of type conversion and the fact that `input()` always produces a string, a common pitfall for beginners. A typical trap is forgetting that even numeric-looking input remains a string until explicitly converted. To remember: think of `input()` as a “string factory”—you must convert its output to `int` or `float` before doing math. A handy mnemonic is “Input strings, convert for things.”
⚠ Common exam trap
Candidates often think `int()` is sufficient, overlooking that shipping costs often require decimal precision, or they may incorrectly believe catching an exception is an acceptable fix instead of correcting the type conversion.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
Change weight = float(input('Enter weight: '))
`input()` always returns a string, and multiplying a string by a float (2.5) raises a `TypeError`. Converting the input to `float` ensures the multiplication is numeric, allowing decimal weights and producing the correct shipping cost.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Change weight = int(input('Enter weight: '))
Why it's wrong here
If weight is decimal, int conversion truncates; use float.
- ✗
Change cost = weight * 2.5 and catch TypeError
Why it's wrong here
Not a robust fix; conversion is better.
- ✗
Change cost = int(weight) * 2.5
Why it's wrong here
If weight is decimal, int conversion truncates; also still could cause error if weight is not numeric.
- ✓
Change weight = float(input('Enter weight: '))
Why this is correct
Converts input to float, allows multiplication.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. You are maintaining a legacy Python 2.7 script that calculates shipping costs. The script reads weight from user input, then calculates cost as weight * 1.5. Recently, the company upgraded to Python 3.9, and now the script raises a TypeError: can't multiply sequence by non-int of type 'float'. The input line is: weight = input('Enter weight: '). You need to fix the script minimally. Which action should you take?
hard- A.Use Python 2 style input by importing from __future__
- B.Change the multiplication to cost = float(weight) * 1.5
- C.Change the input line to weight = int(input('Enter weight: '))
- ✓ D.Change the input line to weight = float(input('Enter weight: '))
Why D: In Python 3.9, `input()` returns a string, not a number. Multiplying a string by a float raises a TypeError. Option D converts the input to a float immediately, which is the minimal fix because it handles both integer and decimal weights correctly without changing the multiplication logic.
Last reviewed: Jun 11, 2026
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.