Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 20 Online
OpenStudy (osanseviero):

Fast python question: Using n= raw_input("enter a number: ") Will turn anything you put to an integer. How can I do so the shell doesn't filter it to integer. I want the person to be able to put anything he wants

OpenStudy (osanseviero):

Someone told me about input()...How can I useit?

OpenStudy (e.mccormick):

2.x line or 3.x line of Python. Also, in the newer Python, raw_input became input because they wanted to get rid of the old method that was problematic.

OpenStudy (e.mccormick):

In short, the old input() should never be used. You should always use raw_input() and convert from a string to the needed number. Why? Because it forces you to account for bad input, which is a common problem.

OpenStudy (anonymous):

The input received from raw_input WILL be of type string not of type int. This lets the user put in anything they want. You can run this simple code to see: x = raw_input('Enter something') print type(x) Converting it from the String type to another type is a different matter.

OpenStudy (e.mccormick):

What nathanprovence is a good point too. The code you wrote will NOT make an int. It will make a string. I ran this: ``` n= raw_input("enter a number: ") print type(n) n= int(raw_input("enter a number: ")) print type(n) ``` and got: enter a number: 5 <type 'unicode'> enter a number: 5 <type 'int'>

OpenStudy (osanseviero):

I want the person to be able to put anything, an integer, a string, a float. And anything it takes, print it

OpenStudy (osanseviero):

Assume that two variables, varA and varB, are assigned values, either numbers or strings. Write a piece of Python code that prints out one of the following messages: "string involved" if either varA or varB are strings "bigger" if varA is larger than varB "equal" if varA is equal to varB "smaller" if varA is smaller than varB So I don't need to make any raw_input, but I had curiosity of how to do it. This exercise will automatically assign different values for varA and varB, both string and numbers. i dont want any specific thing. How will the raw_input (or another function) work? (I need to add it in the start So I did this code ( http://dpaste.com/1426396/) How will be the input/raw_input option?

OpenStudy (osanseviero):

take out the parenthesis

OpenStudy (anonymous):

What you are referring to is usually called input checking. I'm not sure exactly how Python does this, but I did find this: http://answers.yahoo.com/question/index?qid=20100509023327AAqA7GE. It may help you get started. In almost every case, the program you are writing will want to gather a specific input type from the user so that it can do a specific type of work on it.

OpenStudy (anonymous):

raw_input always returns a string, even when numbers are entered. At first, that may seem inflexible, but it actually gives the programmer an opportunity to control how the input is used. You can write code to analyze the string to determine if it would be valid as a float, an int, a bool, or other type, and then do the appropriate conversion, if you choose to do so. One means of handling this is the use "try" and "except" blocks, but there are also string methods that can check the value of a variable or expression, such as isalpha() and isdigit(). If isdigit() returns True, you can use int() to perform a conversion. See the following links: http://docs.python.org/release/2.5.2/lib/string-methods.html http://docs.python.org/2/tutorial/errors.html

OpenStudy (osanseviero):

I will try and update you later :)

OpenStudy (osanseviero):

The isalpha is not working. I don't know how to use try block for this. How can I analyze the string to know if inside there are all numbers, and convert that to a float option, or if they are string, to let it stay that wyay

OpenStudy (anonymous):

@osanseviero: Copy, paste, and try out this example, using Python 2.x ... # trying_raw_input.py # Any type of input will do inp = raw_input("Enter something: ") try: v = int(inp) print "We'll multiply your int by 2." print v * 2 except: try: v = float(inp) print "We'll multiply your float by 3." print 3 * v except: print "We'll treat it as a string." v = inp print "Your string was " + v + "."

OpenStudy (osanseviero):

Thanks a lot, this helps :)

OpenStudy (osanseviero):

int(inp) <- That means the integer input, right?

OpenStudy (anonymous):

I chose the name 'inp' for the variable, because it will contain the value of the string that is input, via raw_input. But another valid identifier name would work just as well. If the variable named 'inp' contains a string that can be converted to an int, the function call, int(inp), will return the int that results from that conversion. But, if it cannot convert that string to an int, the function call will raise an exception. So, if that function call is placed in a try block, and if it cannot convert 'inp' to an int, the except block will execute. That is a means of 'catching' the exception and taking some appropriate action. In this particular case, that action is to try a conversion to a float next. If that also raises an exception, we finally settle for handling the value as a string, which it already is. A try-except structure is not the only way to do this, but it's one of the strategies that works.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!