1

When I try to run this code, I get incorrect maximums and minimums. Could anyone tell me how I can fix it? I am not allowed to use 'max' and 'min'.

UPDATE: I have updated the code and it still doesn't work properly. UPDATE 2: The code works now! Thank you so much guys!

minimum=float('inf')
maximum=None
count=0
total=0
number=input ("Please enter the amount of numbers you wish to categorize: ")
while True:

    num = input("Enter a number: ")
    count+=1
    total+=num

    if num is None or num < minimum:
        minimum = num

    if num is None or num > maximum:
        maximum = num
    if count == number:
        break           

print "The average of your numbers is ", round ((total / count),2),"."
print 'The largest number is:', maximum,"."
print 'The smallest number is:', minimum,"."
9
  • I'm assuming this is homework as you said "I am not allowed to use 'max' and 'min'". In the future, please add the homework tag to any homework questions. Commented Mar 26, 2012 at 1:51
  • 1
    Think about when to initialize minimum and maximum, and about the value you should use. Should it be a value you provide, or should it be a value your user provides? Commented Mar 26, 2012 at 1:53
  • While we're commenting on things, you're using Python 2, in which case / performs truncating division (3/2 == 1). Use float(total)/count or (1.0*total/count) or something to avoid this problem in the case of integer input. Commented Mar 26, 2012 at 2:06
  • 1
    You changed input to raw_input. raw_input returns a string; you have to do this to convert it to an int: num = int(num). Also, as mVChr points out, your break statement is in the wrong place. Think about what happens if your user enters the maximum or minimum number last. Commented Mar 26, 2012 at 2:07
  • I strongly suggest to use int(raw_input( ... )) instead of input. Commented Mar 26, 2012 at 2:15

5 Answers 5

5

Your initial values and conditions for minimum and maximum are incorrect.

minimum = None
maximum = None
 ...
    if minimum is None or num < minimum:
        minimum = num

    if maximum is None or num > maximum:
        maximum = num
...

You could also fix this by checking if count equals 1 instead of identity to None.

Sign up to request clarification or add additional context in comments.

3 Comments

I have changed this, and the maximum and minimum values are still incorrect.
@user1292009, have you read mVChr's answer?
Yes, I just changed that. The maximum value is working fine now, but no matter what, the minimum value is 'None'.
3

In addition to what Ignacio said, you're breaking out of your loop too early. You want to process the last number you enter before breaking, so move the if count == number: break block after the min/max setting blocks.

Comments

1

Note that you can set a number to infinity or negative infinity by

maximum=float('-inf')
minimum=float('inf')

print minimum, maximum

This might be useful for your homework ;)

Ignacio's answer would be preferable if you want to consider the case that the user enters 0 for number (since None would be a more seasonable maximum of no numbers than -inf).

Edit:

Remark to mVChr's correct finding:

Instead of using a while True loop with a break why not writing

while count < number:

or even use a for loop:

for count in xrange(number):

Comments

1

As this is homework you're supposed to learn. Here are some ideas how to solve the problem:

One error is that you're initializing maximum and minimum to 0 on program startup. if you only type in positiv numbers minimum will stay at 0 but won't be the real minimum. To solve this case you should look into lists or initialize both variables to some values that uniquely identifies an invalid initial value that won't leak into your calculation (I suggest None). On the first iteration set both minimum and maximum to the first entered value.

After modfifications: You are comparing strings since raw_input returns strings, not numbers. You have to convert those strings into integer values using int, for example int(raw_input( ... )). Additionally you had a badly indented break of which I fixed the indent.

Other idea: Since you're not allowed to use min and max you might just use

tmp=sorted([int(raw_input('Number: ')) for x in xrange(number)])
minimum, maximum = tmp[0], tmp[-1]

but I guess this defeats your assignment :)

Comments

0

The solution that requires the least treatment of special values, would be to initialize both variables to +/- infinity.

minimum=float("-inf")
maximum=float("inf")

2 Comments

Python doesn't have a reasonable highest (or lowest) integer.
oh, and you must set minimum to inf and maximum to -inf; your answer always gives -inf, inf as answers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.