Why does this code work but if I try to use raw_input it does not: x, y = input("Enter two numbers to find their quotient:") xint = int(x) yint = int(y) print "The quotient of %s and %s is %s" % (xint, yint, xint % yint)
When you use input, Python immediately evaluates the input. In the case of the above code, it takes the users input and treats the two numbers as two integers. It also realizes that you are using one instance of an input to get information for two variables. What raw_input does is takes the input of the user and treats it as a single string. Therefore, if I enter "50,10" as input, a single string is created, and it is "50,10". It is not creating one string "50" and another string "10" that can be converted to integers. A couple of notes on your code: (1) When using input (rather than raw_input) as above, you do not need to convert the input to integers. Python knows that 50 and 10 are integers when the input is received. (2) % is not the operator for integer division. The % operator is modulo, and it returns not a quotient, but the remainder, if any, left after integer division. For division, use the / operator.
What version of python? input and raw_input are changed in 2.x and 3.x
Version 2.6. So is it possible to do this with raw_input?
raw_input in 2.x takes in a string. You will need to split the string to get two seperate strings out of it. Those can then be turned into seperate ints.... which is what cb12 already pointed out. but I wanted to make sure you were just not getting an error from using raw_input in 3.x.
Join our real-time social learning platform and learn together with your friends!