2

I am writing a program using two None-types and for one of them I get an error. Here is the program:

largest = None
smallest =None
while True:
    num = raw_input("Enter a number larger than 0: ")

    if num == "done" :      
        break

    try:
        float(num)
    except:
        print 'Invalid input'
        continue

    if num < 0:
        print'number is smaller than 0'
        continue
    if num > largest:
        largest = num 
    elif num < smallest:
        smallest = num     
    else:
        continue            

str(largest)
str(smallest)
print ('Maximum is '+ largest)
print ('Minimum is '+ smallest)

I always get:

TypeError: cannot concatenate 'str' and 'NoneType' objects on line 23.

The answer may be simple, but I'm just a 12 year old beginner. Please help me.

4 Answers 4

1

The issue is that when you compare num > largest you're initially comparing num > None which will be false. You are also comparing num < None for smallest which will also be false. Therefore when you append it to the end of the string with 'Maximum is '+ largest you are trying to append the None object, which can't be concatenated to strings.

To fix this you have to change your if checks to say

if (largest is None) or (num > largest):

etc.

Then when you go to print them you can do 'Maximum is '+ str(largest) to convert the numbers into string representations that can be concatenated with other strings.

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

2 Comments

It's actually the other way around, None is always smaller than any number in Python 2.
it should be a TypeError honestly, but w/e
0

At the beginning, largest should be 0, not None. Also, smallest should be a very huge number, not None

You need to assign str(largest) to a variable ie.

largest = str(largest)
smallest = str(smallest)

4 Comments

Thanks, that did solve the error, but now I am getting: 'Minimum is None' but largest is giving the right value.. some help?
At the beginning your smallest variable should not be None, it should a very big number, like smallest = 10000 . In other words, it should be greater than your maximum input.
That's what I thought at first but what if we're using the programme for number greater than 1000? If None is the smallest number, is there something that is the largest?
You can use float('inf').
0

Some of the issues in your code -

  1. str() / float() are not in-place functions, they return the converted object back, you need to assign it back to num or largest/smallest .

  2. In Python 2.x, None is always smaller than any integer , so smallest would always be None , you should use float('inf') instead of None for smallest and float('-inf') for largest. Example -

    largest = float('-inf')
    smallest = float('inf')
    

1 Comment

Thanks for the first point, but inf doesn't seem to be built-in. I'm using Python 2.7 by the way.
0

First you don't have to convert to string your numbers you can use like this:

print "Maximum is %r" %smallest

but it might be solve your prolem but this program will not work because you want to compare more than one numbers ("I think") you have to store in an array but I advise to follow this web site If you are new :D

1 Comment

@Yash Mehrotra @ Chad S @ Anand S Kumar @ Ufuk Akoguz Thanks for all your help. I've corrected it and it works like a dream.

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.