Reading Time: 1 minutes
Positives, Negatives & Zeroes using lists in Python: Write a Python script which prompts the user for a sequence of integer values, and once he is finished, displays the list of positives, negatives and number of zeroes he entered. Hint: Use a while loop with a breaking condition for when the user decides to end the sequence of integers.
Positives, Negatives & Zeroes using lists in Python
print("Please enter a sequence of values. Press return/enter key after each sequence. Press q when finished.\n") # Initialize empty lists positives = list() negatives = list() zeroes = list() # Infinite while loop with breaking condition as when-user-enters-q. while True: inputProvided = input("Input: ") if inputProvided == 'q': break enteredNumber = int(inputProvided) if enteredNumber > 0: positives.append(enteredNumber) elif enteredNumber < 0: negatives.append(enteredNumber) elif enteredNumber == 0: zeroes.append(enteredNumber) # Print the list of positives, negatives and number of zeroes he entered. print("Positive values:", positives) print("Negative values:", negatives) print("Number of zeroes:", len(zeroes))
Try it here.