简体   繁体   中英

An error occurs when you try to assign a data type to a input in python

Back Story: I am new to python and was making a very simple program to find the greatest of three numbers while doing so I found it was not giving back the right answer. I thought it might have been a logical issue and pursued to make a flowchart and search examples of the same program online and I found many but I could not find any difference between those programs and mine until I read through it line by line and realized that it was not an issue of the comparison but instead of the method I was taking inputs. I was taking inputs like this:

num1 = float=(input("Enter first number: "))

this extra = sign after the float seemed to fix the problem. However, I was then wondering why python allowed this and if it does what purpose does it serve? What is python actually doing here? And how does it change the outcome of my program?

My program:

num1 = float=(input("Enter first number: "))
num2 = float=(input("Enter second number: "))
num3 = float=(input("Enter third number: "))

if (num1 > num2) and (num1 > num3):
   print("The largest number is",num1)
elif (num2 > num1) and (num2 > num3):
   print("The largest number is",num2)
else:
   print("The largest number is",num3)

My inputs:

 Enter first number: 11
 Enter second number: 6
 Enter third number: 5

The Output:

 The largest number is 6

This line is legal Python, but not very useful:

num1 = float=(input("Enter first number: "))

This is a chained assignment statement, which assigns the value returned by input to two names: num1 and float .

Doing so does not call the builtin float function—in fact, it hides the builtin function, so you can never call it again. (If you try, you'll get an TypeError about strings not being callable.)

This is legal because float and other builtin functions aren't keywords, or anything else special; they're just variables in the builtin namespace.

What you want here is definitely to call float , not to hide it, like this:

num1 = float(input("Enter first number: "))

So, why does it seem to work? Really, it's just coming close to working by accident. Your num1 , num2 , and num3 and up as strings, like the string '6' (and float ends up as the same string as num3 ). It's perfectly legal to compare two strings with > . However, it isn't really what you want. Strings are compared alphabetically, so '6' > '11' (because the first character, 6 , is greater than the first character, 1 ). That's why you want to convert these strings to numbers: the float 6.0 < 11.0 , just as you'd expect for numbers.

If you were having problems in your code, adding an = here is not the way to fix them. At best, you're just hiding the bug behind another bug so you don't even get to the real problem anymore.

With the extra = , you're assigning the returning value of input() to float as a variable, shadowing the built-in float() function. The value is then assigned to num1 (and num2 , num3 in the following lines).

You should remove the extra = s so that the inputs can be properly converted to numbers for numeric comparisons; otherwise your comparisons will be based on strings, which is why '6' is considered larger than '11' in your example output.

num1 = float = (input("Enter first number: "))

This code reads the user's input and assigns that value to both num1 and float .

You probably wanted this instead:

num1 = float(input("Enter first number: "))

As it stands, your code is comparing num1 , num2 and num3 as strings, in which case 6 is indeed the "largest" string.

By assigning num1 , num2 , num3 and float to input functions you will get following variables:

num1 = '11'
num2 = '6'
num3 = '5'
float = '5'

The = operator is an assignment operator which helps you to shadowing the float funtion. With using it, you set a new value to float which sets to '5' with last input. With this values, you compared three strings and '6' is greater than '11' so you get num2 as the largest input. Shadowing a builtin function is not recommended, but it is a flexibility of python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM