user11093215
user11093215

Reputation:

Is there a way to make this code more "Neat"?

The user inserts multiple values and the program determines how many positive values, negative values, zero values, even values, odd values, and the min, max, average, and total of those values. The code works, however it's quite messy.

negative=0
zero=0
positive=0
even=0
odd=0
max=0
min=0
a=int(input('Number of values:'))
b=int(input('First value:'))
if b==0:
    zero=1
if b<0:
    negative=1
if b>0:
    positive=1
if b%2==0:
    even=1
else:
    odd=1
if b>max:
        max=b
total=b
for x in range(a-1):
    b=int(input('Next value:'))
    total+=b
    if b==0:
        zero+=1
    if b==0:
          zero+=1
    if b<0:
          negative+=1
    if b>0:
          positive+=1
    if b%2==0:
          even+=1
    else:
         odd+=1
    if b>max:
        max=b
    if b<max:
        min=b
print('Negative values:',negative)
print('Zero values:',zero)
print('Positive values:',positive)
print('Even values:',even)
print('Odd values:',odd)
print('Total:',total)
print('Minimum:',min)
print('Maximum:',max)
print('Average:',total/a)

Upvotes: 0

Views: 148

Answers (2)

Green Cell
Green Cell

Reputation: 4783

There's two parts of the script essentially doing the same thing, except that you're asking the user "First Value" instead of "Next value". So I would just merge them together in a loop and determine what msg to use.

There's also a lack of white space everywhere which makes it harder to read. Check out Python's PEP 8 guide to standard conventions.

You can also use more descriptive variable names so it's more readable at a glance.

Also be careful naming your variables min and max. Those are already built-in methods in Python that you're overriding.

negative = 0
zero = 0
positive = 0
even = 0
odd = 0
maxVal = 0
minVal = 0
total = 0

count = int(input('Number of values:'))

for i in range(count):
    if i == 0:
        msg = 'First value:'
    else:
        msg = 'Next value:'

    val = int(input(msg))

    total += val

    if val == 0:
        zero += 1

    if val < 0:
        negative += 1

    if val > 0:
        positive += 1

    if val % 2 == 0:
        even += 1
    else:
        odd += 1

    if val > maxVal:
        maxVal = b

    if val <= minVal <= val and val < maxVal:
        minVal = val

print('Negative values:', negative)
print('Zero values:', zero)
print('Positive values:', positive)
print('Even values:', even)
print('Odd values:', odd)
print('Total:', total)
print('Minimum:', minVal)
print('Maximum:', maxVal)
print('Average:', total / count)

Upvotes: 1

Jab
Jab

Reputation: 27515

I made a rendition of your code that in my mind is neater, this way you aren't just storing values you have a Counter that remembers all the values put in and the number of times they were used. Also fixed your whitespace and times_to_run is more descriptive than a and value is more descriptive than b. I also just used the iteration number you're on as the n'th number being input

from collections import Counter

total = 0
all_numbers = Counter()

times_to_run = int(input('Number of values: '))

for x in range(times_to_run):
    value = int(input(f'Value {x + 1}: '))
    total += value
    all_numbers[value] += 1

negative = sum(all_numbers[n] for n in all_numbers if n < 0)
zero = all_numbers[0]
positive = sum(all_numbers[n] for n in all_numbers if n > 0)
even = sum(all_numbers[n] for n in all_numbers if abs(n) % 2)
odd = sum(all_numbers[n] for n in all_numbers if not abs(n) % 2)
max = max(all_numbers)
min = min(all_numbers)

print()
print('Negative values:',negative)
print('Zero values:',zero)
print('Positive values:',positive)
print('Even values:',even)
print('Odd values:',odd)
print('Total:',total)
print('Minimum:',min)
print('Maximum:',max)
print('Average:',total / times_to_run)

To test:

Number of values: 10
Value 1: -1
Value 2: -1
Value 3: 5
Value 4: 2
Value 5: 0
Value 6: 3
Value 7: 55
Value 8: 8
Value 9: 10
Value 10 :4

Negative values: 2
Zero values: 1
Positive values: 7
Even values: 5
Odd values: 5
Total: 85
Minimum: -1
Maximum: 55
Average: 8.5

Upvotes: 0

Related Questions