Task: Check if an integer is positive or negative.
Check if the integer is divisible by 2 or not.
When 0 is entered I need to exit the loop and report each of the counts and sums. No output is displayed from the print function.
This is the code I’m using starting from the def main()
def main():
count positive = 0
count negative = 0
count_divisible_by_2 = 0
sum positive = 0
sum negative = 0
sum_divisible_by_2 = 0
while True:
try:
number = integer(input("Enter a integer(or the number 0 to stop): "))
except `Value Error`:
print("Invalid integer. Please enter a valid number value. :")
print ("Enter the correct integer")
if number == 0:
break
if number > 0:
count positive += 1
sum positive += number
else number < 0:
count negative += 1
sum negative += number
if number % 2 == 0:`
count_divisible_by_2 += 1
sum_divisible_by_2 += number
print(f "Positive integers count: {count positive}, sum: {sum positive}")
print(f "Negative integers count: {count negative}, sum: {sum negative}")
print(f "Integers divisible by 2 count: {count_divisible_by_2}, sum: {sum_divisible_by_2}")``
The output expected is the positive count, negative count, and if the integer is divisible by 2.
New contributor
S L is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.