1

The following is my code that calculates <, > incorrectly. About 3X into giving raw_input it will do things like value 12 < 4. I have added several float commands to try to keep it from having problems with string and int. I'm very new to coding.

largest = None smallest = None while True: num = raw_input("Enter a number: ") if num == "done" : break try: float(num) except ValueError: print "Invalid input" break float(num) if largest is None: largest = num float(largest) if smallest is None: smallest = num float(largest) if num > largest: largest = num float(largest) if num < smallest: smallest = num float(smallest) print num print "Maximum", largest print "Minimum", smallest 
4
  • 3
    What does "do things like value 12 < 4" mean? Commented Mar 25, 2016 at 17:46
  • 5
    you're not comparing 12 < 4; you're comparing "12" < "4" Commented Mar 25, 2016 at 17:46
  • 3
    Also, just saying float(smallest) doesn't change smallest. You need to do smallest = float(smallest). Commented Mar 25, 2016 at 17:47
  • @zondo is correct. In your case, if you just modify the try block to try: num = float(num) then i dont think you will have to make any other changes. Just that you can remove all the float() calls. Commented Mar 25, 2016 at 18:04

4 Answers 4

1

float(num) computes the floating-point value of num, but doesn't do anything with it; in particular, it doesn't change the value of num. You'll need an assignment to do that.

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

Comments

1

float does not change the variable in-place - it returns a cast value, which you are ignoring by not saving it anywhere. Just assign it, and you should be OK:

try: num = float(num) except ValueError: print "Invalid input" break if largest is None: largest = num if smallest is None: smallest = num if num > largest: largest = num if num < smallest: smallest = num print num 

Comments

1

In your current code, after casting num as a float, you never actually assign it to anything.

float(num) 

Instead you will want to re-assign num after the conversion

num = float(num) 

If you don't do this (as in your current code), when you're performing the comparisons, you're doing a string comparison rather than a numeric comparison.

"12" < "4" # True 

3 Comments

I think you need to remove that initial conversion to float in order to remove the downvote on your answer, since the user is using the input given to a string "done". Just my wildest guess though!
@Sнаđошƒаӽ Yup! Just realized that. Thanks for pointing that out.
@Cyr1lfiggus If Suever answerd your question, please mark their answer as accepted (green check).
0

You don't change the value in num in your code. Typing "float(num)" doesn't really do anything because it's just temporarily casting that variable as a floating point number. To actually change the value I suggest the following edits.

largest = None smallest = None while True: num = raw_input("Enter a number: ") if num == "done" : break try: float(num) except ValueError: print "Invalid input" break num = float(num) if largest is None: largest = num if smallest is None: smallest = num if num > largest: largest = num if num < smallest: smallest = num print num print "Maximum", largest print "Minimum", smallest 

1 Comment

Thank you very much for the quick feedback!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.