233k views
4 votes
) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point. If entry has no comma Output: Error: No comma in string. (1 pt) If entry has more than one comma Output: Error: Too many commas in input. (1 pt) If entry after the comma is not an integer Output: Error: Comma not followed by an integer. (2 pts)

1 Answer

1 vote

Answer:

In Python:

entry = input("Sentence: ")

while True:

if entry.count(",") == 0:

print("Error: No comma in string")

entry = input("Sentence: ")

elif entry.count(",") > 1:

print("Error: Too many comma in input")

entry = input("Sentence: ")

else:

ind = entry.index(',')+1

if entry[ind].isnumeric() == False:

print("Comma not followed by an integer")

entry = input("Sentence: ")

else:

break

print("Valid Input")

Step-by-step explanation:

This prompts the user for a sentence

entry = input("Sentence: ")

The following loop is repeated until the user enters a valid entry

while True:

This is executed if the number of commas is 0

if entry.count(",") == 0:

print("Error: No comma in string")

entry = input("Sentence: ")

This is executed if the number of commas is more than 1

elif entry.count(",") > 1:

print("Error: Too many comma in input")

entry = input("Sentence: ")

This is executed if the number of commas is 1

else:

This calculates the next index after the comma

ind = entry.index(',')+1

This checks if the character after the comma is a number

if entry[ind].isnumeric() == False:

If it is not a number, the print statement is executed

print("Comma not followed by an integer")

entry = input("Sentence: ")

If otherwise, the loop is exited

else:

break

This prints valid input, when the user enters a valid string

print("Valid Input")

Note that: entry = input("Sentence: ") is used to get input

User Andrew Moylan
by
5.4k points