I thought the input method will take any numeric or string input and print it out. But for string it does not work unless string is in quoted. Why?
In Python 2.7, input() evaluates the input as code, so strings need to be quoted. Python 2.7 has a method called raw_input() that treats all input as strings (no quotes needed).
In Python 3.x, the Python 2.7 raw_input() method was renamed input() and the Python 2.7 input() functionality was replaced by eval(input())
So you can use raw_input() in Python 2.7 or switch to Python 3.x
Make sure that you are running the code on Python 3 and not Python 2.
On python 2 you are needed to quote your input with ""
it seems like there are multiple solution to this though:
input() error - NameError: name '...' is not defined
Related
I am new to python programming. I am using latest version of pyCharm and doing remote debugging of some code which is on raspberrypi. There is this code.
# Get the verifier code from the user. Do this however you
# want, as long as the user gives the application the code.
verifier = input('Verifier code: ')
Where I enter some string like 117-820-181 on the console window and in the veriifer variable it shows up as an int. on the next line the code breaks as it expects verifier to be string and not int.
Any ideas why this is returning int instead of string ?
I believe that you are using Python 2.x (input in that version evaluates the given input as code, which is what seems to be happening. Also, I think that PyCharm likes to use Python 2.x rather than 3.x).
If this is so, then use raw_input() (which returns everything as a string, much like Python 3.x’s input() function does):
verifier = raw_input('Verifier code: ')
This will stop the issue where the verification code is turned into an integer.
Make it a string on input
verifier = str(input('Verifier code: '))
I'm just a beginner in python.
I was just writing some code to print table of any number and I surprisingly got different answers on python v2.7 & python v3.5!
This occur only when I use string function, not when I use comma(,).
The difference is the type of 'n'. It is treated as an integer than you get the numeric operation '', in the other case it is treated as string with the string operation ''. Try out type(n) after you read it and you will see the difference.
I'm writing code that's supposed to be Python 2.7 and Python 3.3+ compatible. When trying to run my code with Python 2.7, I get the following problem.
I'm importing unicode_literals from __future__ in each file and I'm having trouble getting the array function to work.
from array import array
from __future__ import unicode_literals
Trying to make a character array doesn't work
array("c", "test")
> TypeError: must be char, not unicode
Trying to make a unicode array also doesn't work
array("u", "test")
> TypeError: must be char, not unicode
Can I make an array that is compatible with unicode_literals?
This error is being thrown because of the first argument to array(), the typecode. In Python 2 this must be a non-unicode character (string of length 1), and in Python 3 this must be a unicode character. Since both of these are the meaning of str in the respective Python versions, this works in both:
array(str('u'), 'test')
This is a limitation of the array module that was fixed recently (https://bugs.python.org/issue20014). On Python 2.7.11 (or newer) the array constructor will accept both str and unicode as the first argument.
As a workaround you can use e.g. array(str("u"), "test"). I'm referring to the other answer by Dan Getz for an explanation of why this works.
Note that your first example using the "c" typecode still won't work on either Python 2.7 or Python 3.x. On Python 2.7, you need to pass a bytestring as the second argument (e.g. by passing b"test" as the second argument). The "c" typecode was removed in Python 3.0, so you should use "b" or "B" instead.
I am brand new to python. I have been working on the courses on Codecademy. I am also currently using Pydev / LiClipse.
In one of the first lessons on Codecademy it wants you to set the variable parrot to "Norwegian Blue". Then it wants you to print the length of parrot using the len string method. It is very simple, and I got the answer right away with:
parrot = "Norwegian Blue"
print len(parrot)
When I put the exact same code into LiClipse it returned:
SyntaxError: invalid syntax
It work in LiClipse when I changed it to:
print (len(parrot))
Can someone let me know why that worked in codecademy, but not in LiClipse, and why adding the parenthesis fixed it?
It sounds like Pydev/LiClipse is using Python 3 while Codeacademy is using python 2.x or some other older version. One of the changes made when python updated from 2.x to 3 was print is now a function.
Python 2:
print "stuff to be printed"
Python 3:
print("stuff to be printed")
You must take into account the version in which you are working.
In Python 2 your code would look like this:
parrot = "Norwegian Blue"
print len(parrot)
In Python 3 your code would look like this:
parrot = "Norwegian Blue"
print ( len(parrot) )
It worked in CodeAcademy because their interpreter is a Python 2.7, where you didn't need the parenthesis because print was a statement. In Python 3.0+, print requires the parentheses because it's a function.
More information on what's different between Python 2.7 and 3.0+ can be found here:
What's New In Python 3.0
Some of the sample differences with print on the above page:
Old: print "The answer is", 2*2
New: print("The answer is", 2*2)
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
Old: print # Prints a newline
New: print() # You must call the function!
It's good to know the differences between both, in case you're working with legacy systems and the lot vs. in your private environment. In Python 2.7 and below, print() works; however, omitting the ()s does not work in Python 3.0+, so it's better to get into the habit of using them for print.
End of life for Python 2.7 is expected to be in 2020, so you have plenty of time anyway.
In Python 3 print was changed to require parenthesis. CodeAcademy is probably using Python 2 and it looks like you're using Python 3.
https://docs.python.org/3/whatsnew/3.0.html#print-is-a-function
From the docs
Print Is A Function
The print statement has been replaced with a
print() function, with keyword arguments to replace most of the
special syntax of the old print statement (PEP 3105). Examples:
use=input('what do you wanna do \n1.press w to create a new file\n2.press r to read a file:\n')
if use=='r':
read()
elif use=='w':
write()
else :
print('OOPS! you enter a wrong input\n')
user()
when i run this code using IDLE it runs properly but when i created a exe of this python file using cx_freeze then the if and elif conditions are not working for 'r' and 'w' respectively. for any input it always goes to the else statement.
I am using python 3.2 and cx_freeze 3.2
Just for a quick test, I did this:
use = input("test input here: ")
for i in use:
print(ord(i))
The result, if you type in "hello", is the ascii character codes for hello, plus "13". This is \r, the return character, which is being added to your string. This doesn't happen under Linux and is the result of the fact on Windows a newline is \r\n as opposed to just \n.
The workaround for you would be to do something like:
use = input("test input: ").strip("\r")
strip() is a string object method that'll remove characters from the end and beginnings of strings.
Notes:
The use of ord() in the above example is probably not best practise - see Unicode.
If you ever write GUIs and use cx_freeze, don't use print() or input() - on Windows the standard input/output handles don't exist for GUI apps at all. That tripped me up for a while with cx_freeze + gui code. Just a note for when you get there.