Start Your Journey with Linux Command Line
| empty lines in python |
If you have reached practice 2.5.1.3 Four simple programs, you will notice a challenge in the bottom of the exercise:
The code has one important weakness - it displays a bogus result when the user enters an empty line. Can you fix it?
Let's see how to fix it.
User may enter an empty line or just press Enter without any data.
In LAB 2.5.1.3 Four simple programs, the starting code reads a line of space-separated numbers, splits it into a list of strings with line.split(), and sums them with float(). The weakness appears the moment the user simply presses Enter without typing anything: input() returns an empty string, and "" split by spaces produces an empty list []. Because there are no numbers to process, the loop sum is never triggered, yet the program still moves on and prints a supposed total that is meaningless or zero. That bogus result is exactly what the exercise asks you to fix.
The clean way to solve the problem is to keep re-prompting until the user actually supplies data. You do this with a while loop whose condition checks whether the current split list is still empty, combined with an else clause that only runs once genuine input has been received. As long as strings equals [], the program asks again with a helpful message such as "No data found"; the moment a real line arrives, the loop ends and the sum is calculated safely.
The favorite pattern here pairs a while loop with its optional else. In Python, the else attached to a while loop executes only when the loop finishes normally, meaning the condition became false without an explicit break. Inside the loop you reassign line = input(...) and recompute strings = line.split(), so each failed attempt updates the state that the condition inspects. Only when strings is no longer empty does control fall through to the else, where the for loop converts each substring with float() and accumulates the total.
A second gotcha lives inside that summing step. Because whitespace between numbers can be inconsistent, the code also wraps the conversion loop in a try/except. If some substring cannot be converted to a number, the ValueError is caught and the offending item is reported instead of crashing the whole program. This layered handling shows how a single lab can combine input validation, loops, and exception handling into one robust little script.
[], which is why the original program produced a bogus result.while loop with a condition testing that list, plus an else clause, keeps prompting until the user enters real data.input() and rebuilds the list with line.split() so the loop condition can be re-evaluated.try/except catches non-numeric items and reports them instead of crashing.else of a while runs only when the loop finishes normally, without hitting a break.Calling split() with no arguments on an empty string returns an empty list [], because there are no words to separate. The program then has nothing to sum, which is why it needs to detect that state and ask again.
The else attached to a while loop executes only when the loop's condition becomes false normally. It does not run if the loop is exited with a break. Here it safely starts the summing code once valid input has arrived.
Not robustly. Without a loop the program would accept the first (possibly empty) input and still print a meaningless result. Continuously re-prompting is what guarantees genuine data before any calculation happens.
It protects against a ValueError raised when float() is given a substring that is not a valid number. The exception is caught and the offending string is printed rather than terminating the program.
Comments
Post a Comment
Your opinion matters, your voice makes us proud and happy. Your words are our motivation.