Invalid Syntax in Python 3.5.2 using Codeacademy functions - python-3.x

I'm trying to code in Python 3. So far I've used codeacademy to copy and paste the functions I've wanted. Unless what codeacademy uses is python 2 (which it's not, I've checked). So I'm curious why it highlights len and says invalid syntax.
print ('Have you thought today?')
original = raw_input('Yes or No:')
If len(original) > 2:
print ('When?')

You put If capitalized. Python is case sensitive so you must use the correct keyword which is lowercase if.
Also raw_input() was renamed to input() in Python 3.

python3 doesn't have raw_input().
Change
original = raw_input('Yes or No:')
to:
original = input('Yes or No:')
And, also use if in place of If.

Related

How to change a string into a variable

I want to write out some data into a file. I saved the filename as a variable. I wan to use % mode to substitude the variable to the text, but it gives an error:
IndentationError: unindent does not match any outer indentation level
writeafile = open('N:\myfile\%s.txt' , "a") % (variable)
Assuming we are talking about Python here, you should move variable next to the
'N:\\myfile\\%s.txt' string for correct syntax, like so:
writeafile = open("N:\\myfile\\%s.txt" % variable, "a")
However, using this style of formatting is not recommended by Pydocs:
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals, the str.format() interface, or template strings may help avoid these errors. Each of these alternatives provides their own trade-offs and benefits of simplicity, flexibility, and/or extensibility.
Source
So, I'd suggest using f-strings, which have been available in Python since 3.6. The double \\ is intentional here, otherwise Python will treat it as an escape character and you'll get undesired results.
writeafile = open(f"N:\\myfile\\{variable}.txt", "a")
Alternatively, you could also use str.format():
writeafile = open("N:\\myfile\\{name}.txt".format(name=variable), "a")

What is the good way to get output with u prefix in python3

What is the good way to get string output with u prefix in python3?
I'd like to use a library to do this like six.
I'm fixing assert testing with string output under compatible with python2.
import six
assert(repr(six.text_type('TEST TEXT')) == "u'TEST TEXT'")
For python 3, strings are unicode by default, so you won't see the u prefix.
For testing purposes, you should simply compare the values, not the representation.
expected = u'TEST TEXT'
assert(actual == expected)
In Python3, you can use String formatting to set the 'u' prefix. Please see the example below:
print('u{}'.format("TEST TEXT"))
Output will be: uTEST TEXT
Please note that this will only work in Python 3 and above.
Thanks!

Why does this python code produce an error:?

Basically in python 3.0 I tried to use two %s string substitutes and concatenate it. However, it seems to produce an error.
CODE
print "%s"+"%s" %("John", "rows")
I am new to programming so I would be thankful if I could get a simple explanation.
Thanks
In python-2.x, which appears to be the language that line of code was written in (you treated print as a statement), you will get an error TypeError: not all arguments converted during string formatting.
This is because the interpreter attempts to put both of the arguments into the latter string, due to the order of operations. Thus, simply wrap the string with parentheses and the code will run:
>>> print ("%s"+"%s") %("John", "rows")
Johnrows
>>>

How to get a 01 string through input()?

I want to get a string which merely consists of 0 and 1.
Since raw_input() was renamed to input() in Python 3, I wrote:
buf = str(input())
print(buf)
However,when I tried '00101', buf surprisingly turned out to be '65'.
Why this happened and how to prevent this?
As a beginner in Python, it confuses me so much.
update: Problem solved. See my answer below.
i try this statement in python3 and output was 00101
btw: output from function input() is string, you dont need call str() on input()
but if you run input() in python (python 2.7) you get output 65
Problem solved. I found that I forgot to remove Python 2.7 directory from environment variables. Furthermore, a number begins with zero is considered as an octal one. That's why I got '65' for '00101' in Python 2. Thank you all.

Python3.4 Anaconda: Input() Function Broken?

I'm having trouble with the input() function in Python3.4 using the Anaconda integrated editor. If I just type
x = input()
into the editor, it returns a blank line that I can type text into. If I type:
foo
into this line, I would expect 'foo' be stored as a string with variable name x. But, instead I get:
NameError: name 'foo' is not defined
To make the function work as expected, I must instead type in:
'foo'
which is unfortunate because what I really want is just to pause my code and wait for an arbitrary user input, and I read somewhere that "wait = input()" is the most pythonic way to do this. Using that line in my actual script returns an "unexpected EOF" error - I assume as another symptom of the same problem. Can anyone suggest a workaround?
Note: I suspect this is an Anaconda-specific problem, given the following reference:
https://docs.python.org/3.4/library/functions.html#input
Thanks for your time.
Your code is being run by Python 2, not 3. I don't know enough about Anaconda to know if the problem is with their editor, or if you have your path messed up, but the problem is that the wrong version of Python is being used.

Resources