I have a question on assignment 0. I wrote the code successfully, but not in the way I would have expected. I tried: print (raw_input ('what is you last name')) last = raw_input ('what is your last name?') print (raw_input ('what is your first name?')) first = raw_input ('what is your first name?') print first , last I thought the line "last = raw_input ('what is your last name?')" was just assignment. But when I ran the script, it asked for first and last name twice. Can anyone explain why they are printing? Thanks!
You are printing what is input, then seeting what is input to something.
And what version of Python are you using?
Thanks for replying, but I am not sure what your first post means. I am going off of the description of input in the following: http://en.wikibooks.org/wiki/Python_Programming/Input_and_Output. That page describes asking for input by "print (raw_input ('what is your name?'))" then assigning a variable to the input by : "x = raw_input ('what is your name?')" Are you saying that is an error on the Wikibooks page or am I just misunderstanding the page? I am using Python 2.5.4 because that's what is used in the OCW lectures.
It is doing it twice because you are doing it twice, and I bet you have 2.7.x because that version knows both ways of making print work.
``` print (raw_input ('what is you last name')) # <- print last = raw_input ('what is your last name?') # <- assign to last print (raw_input ('what is your first name?')) # <- print first = raw_input ('what is your first name?') # <- assign to first print first , last # <- print first and last ```
That's exactly what I did in my first attempt and it asked for my last name twice and my first name twice.
Yes. Because you told it to.
I know that your answer is correct because that's what happens when I run my script. But it conflict with statements from the professor, the readings, and the WikiBooks page linked below. During the lectures, the professor says that assignments won't print nor will anything print in a script unless it is told to do so. So why is my assignment of "last = raw_input ('what is your last name?') printing?
If you have version 2.7.x, it does not work the same as thr 2.5.x used in that class.
If you are using 2.5.4, it should have given you errors.
That can't be right because I have 2.5.4 and followed what was described in the course materials.
I just installed 2.5.4, and oddly the print() function works. I thought that was added a bit later. At some point in the 2.x line, print was not a function.
So, that change was pre 2.5.4. Hmm. So I am wrong on that, but you still told it to do things twice.
To expand on what Lyrae did: ``` # do a raw input. Return the results to the print statment so it prints it. print (raw_input ('what is you last name')) # do a raw input. Assign the results to last last = raw_input ('what is your last name?') # do a raw input. Return the results to the print statment so it prints it. print (raw_input ('what is your first name?')) # do a raw input. Assign the results to first first = raw_input ('what is your first name?') # print first and last print first , last ```
raw_input() is a function. A function is multiple commands to do a specific task. In the case of foo = raw_input( "Hello World" ), the function outputs the argument that it's given, in this case "Hello World" and returns what the user types in which is assigned to the variable foo. Note: raw_input() returns user input as as string, so if you want numbers, you'll have to convert it.
Join our real-time social learning platform and learn together with your friends!