String Formatting works in CodeCademy, But not Python itself


#1


So in the exercise String Formatting Part 2, I completed the exercise, and in full belief I had mastered the concept of the %s function I fired up Notepad++ on my PC and wrote some code of my own and when I executed it via Python on my PC rather than thru CodeCademy's online interpreter, ERROR msg. I had to triple check the code a million times, I saw no mistakes, so I copied the EXACT code from CodeCademy into my Python file, saved it, and ran it. Same problem. Error: "missing parenthesis called to print"
I've check it over and over and it's the EXACT same code that is executing correctly via CodeCademy online Python interpreter. Any explanation for this??

My PC is running Python 3.5.1
The only thing I can think of is maybe the CodeCademy online interpreter is a different version then what im running on my PC. And for whatever reason something in the new version of Python doesn't like the format or syntax of the code below.
Any idea's?

TLDR; the code below will execute via codeCademy online, but not in Python itself installed on my PC.

Thanks


name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)


#2

name = str(input("What is your name? "))
quest = str(input("What is your quest? "))
color = str(input("What is your favorite color? "))

print ("Ah so your name is {0:1}, your quest is {1:1}, and your favorite color is {2:1}".format(name, quest, color))

try this.


#3

Python 3.x does not use raw_input(), only input(). print is print() and the modulo formatting method has been replaced with the string.format() method.

If we run your code from above, the first error is thrown during compile, missing parentheses on call to print.

We fix that, and Run, This time we get a runtime exception raised:

Traceback (most recent call last):
  File "D:/Python35/test_27in35.py", line 1, in <module>
    name = raw_input("What is your name?")
NameError: name 'raw_input' is not defined

When we fix that error, and Run...

What is your name?roy
What is your quest?enlightenment
What is your favorite color?green
Ah, so your name is roy, your quest is enlightenment, and your favorite color is green.

So the legacy string formatting is still supported. I'm not informed enough to say how much longer it will be supported, or if support will ever be removed.


#4

input() returns a string so we do not need to cast it to str().


#5

A post was merged into an existing topic: String Format


#6

Firstly thanks for all the replies, but I must say this is all highly confusing to someone trying to learn the language. Now it raises more questions than answers such as;
1. I've heard raw_input is better than input() because input() has security flaws. (source: LPTHW.com)
2. What version of Python should I acutally be using!?!?
3. So if I write a Python program using the 3.x format/syntax, on some peoples computers it may Not RUN because they have Python 2.x??
4. Why are they doing this to ME!?! lol...as if this stuff wasn't confusing enough.


#7

im using python3 but im also in the same boat as you and getting told many different formats to write ive heard raw_input but then got told input replaces it and that raw input is better and to keep using it str(input) ive been using for alot of my code i only started learning last week but im going go with what this guy mtf says he seems experienced and im going use his way of laying out the code as its been affective this way.

i also watched videos but not have explained as good as mtf by the way pay no attention to my code that's just a test script i made to try and learn the lang sorry if it has caused confusion.


#8

Can you show me the fixed code so i can see exactly what you did there? thank you


#9

This is something I just found from Learn Python The Hard Way, I'm going to share it here because it seems to be relevant to my issue and possibly yours also. Maybe some of more Experienced Pythonista's can elaborate on the following excerpt from Learn Python The Hard Way..

"A programmer may try to get you to install Python 3 and learn that. Say, "When all of the Python code on your computer is Python 3, then I'll try to learn it." That should keep them busy for about 10 years. I repeat, do not use Python 3. Python 3 is not used very much, and if you learn Python 2 you can easily learn Python 3 when you need it. If you learn Python 3 then you'll still have to learn Python 2 to get anything done. Just learn Python 2 and ignore people saying Python 3 is the future."

Thoughts anyone?


#11

name = input("What is your name?")
quest = input("What is your quest?")
color = input("What is your favorite color?")

print ("Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color))

The above runs fine in 3.5. On CC we are working in 2.7.x so bear that in mind. For our purposes here we use raw_input() in all exercises. You can use print () in 2.7 so go ahead and write it that way for practice and to form a habit.

As mentioned, only applies to 2.7.x.

It depends on what support is needed. In some rare cases a program may depend upon modules that are present in 2.7 but not in 3.5. Not likely going to an issue for us beginners, though. It can't hurt to learn both old and new, but moving forward you will probably find 3.5 becoming more common as the community grows.

Most people don't have Python on their computers, and any who do will be able to tell what version they need to be running to run your code. Not an issue, as I see it.

It's called progress. Technology is changing constantly and program languages will continue to mature and mutate to accommodate the latest accepted trends.


#12

A post was merged into an existing topic: String Format


#13

Thanks a bunch Mtf you are a rockstar.
I think im going to switch to Python 2.7 on my PC to follow along better with CC, and for all the reasons LPTHW mentioned also.
Those tiny changes are messing with me, and once I learn Python 2.7 i can always learn the little variations that come with Python 3.x
Again thanks for your input, it has helped me through this issue.


#14

Keep in mind the print () thing. It works in Python 2.7 and there are very few if any lessons that will not accept the new syntax. That will be one 'habit' that you will be able to carry forward and avoid the most common error in making the transition.

Learning Python 2 is like learning HTML. Learning Python 3 is like learning HTML5. The core language is solidly entrenched in earlier versions. Learning them in reverse order is a painful way to learn. It's like trying to learn jQuery before learning JavaScript. Another hair puiler.