Question 400 of 510
Data Types, Variables, Basic I/O and OperatorshardMultiple ChoiceObjective-mapped

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.”

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

This PCEP practice question tests your understanding of data types, variables, basic i/o and operators. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

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?

Question 1hardmultiple choice
Full question →

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: '))

Option D is correct because `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.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

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.

    Related concept

    Read the scenario before looking for a memorised answer.

Common exam traps

Common exam trap: answer the scenario, not the keyword

The trap here is that candidates may 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.

Detailed technical explanation

How to think about this question

In Python, the `*` operator is overloaded: for strings, it repeats the string when multiplied by an integer, but multiplying a string by a float raises `TypeError` because repetition by a non-integer is undefined. Converting the input to `float` first ensures the operation is numeric multiplication, which works seamlessly with both integers and decimals. This is a common pitfall in user-input handling where type conversion is forgotten.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A practitioner preparing for the PCEP exam encounters this exact type of scenario on the job. The correct answer here is not the most general option — it is the best answer for the specific constraint described. Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option. Real exam questions reward reading the full scenario before eliminating options, because the constraint defines which answer fits.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCEP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCEP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCEP question test?

Data Types, Variables, Basic I/O and Operators — This question tests Data Types, Variables, Basic I/O and Operators — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: Change weight = float(input('Enter weight: ')) — Option D is correct because `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.

What should I do if I get this PCEP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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 →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

1 more ways 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.

Keep practising

More PCEP practice questions

Last reviewed: Jun 11, 2026

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.

Loading comments…

Sign in to join the discussion.

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.