Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

Is this correct? (Problem set 0). Warning: I have never had any experience in this field whatsoever. >>> print raw_input('Enter your last name:') Enter your last name: >>> x = raw_input('Enter your last name:') Enter your last name:Spielberg >>> print raw_input('Enter your first name:') Enter your first name: >>> y = raw_input('Enter your first name:') Enter your first name:Steven >>> print y,x Steven Spielberg

OpenStudy (anonymous):

The language looks like some flavor of BASIC, but I don't recognize it. What does your book/prof call it? Beyond that, it looks wrong to me. I think you're having a problem understanding how variables work in programming. Let me show you an example of how you could do what you're trying to do if you used Microsoft's QuickBasic: INPUT "Enter your last name: ", x$ INPUT "Enter your first name: ", y$ PRINT y$; " "; x$ This code will work in most versions of BASIC running around out there. The Dollar signs define x and y as "string variables". These are used for literal strings such as "Steven" and "Spielberg". The "INPUT" command tells the interpreter to accept keyboard input and place it into the variable that follows. Running the above program will first print out "Enter your last name: ", and then it will wait for you to type "Spielberg" or some such. When you hit the enter key, the program will store "Spielberg" in x$. The program will then display the next line which is "Enter your first name: ", and it will again wait for input from the keyboard, such as "Steven", which it will then store in y$. The program will then execute the next line of code which is to print out THE CONTENTS of y$ followed by THE CONTENTS of x$. Since x$ has "Spielberg" stored in it and y$ has "Steven" stored in it, then printing out y$ followed by x$ will print out "Steven Spielberg". Now depending on the make and model of whatever BASIC your class is using, your code may require alteration to comply with its own way of doing things, but I hope you can see from my example how the variables work with data.

OpenStudy (anonymous):

The MIT 600 course uses python not basic. When I look at PS0 it asks for date of birth and last name and then prints them in reverse order. Basically the same as what you did though. The \n puts in a line feed. >>> x = raw_input('Enter your date of birth:\n') Enter your date of birth: 01/26/32 >>> y = raw_input('Enter your last name:\n') Enter your last name: Grimson >>> print y, x Grimson 01/26/32

OpenStudy (anonymous):

Of course you could write those 3 lines of code in a file instead of in the immediate window and then run the file as well.

OpenStudy (anonymous):

@msmithhnova Ah, "python"! Check! I've never fiddled with that language before. So it now appears to me that "raw_input" is a python command roughly equivalent to basic's "input", and not a poorly manifested name of a variable as I originally thought. :-) The formatting of his original post was also confusing, and I now see what a difference the "new line" makes! :-) So @DrKingSchultz, it appears that you were substantially correct, although I think your two "print raw_input" lines are redundant and unnecessary. So altering your code to remove those 2 lines and adding in "new line" characters would yield: >>> x = raw_input('Enter your last name:\n') Enter your last name: Spielberg >>> y = raw_input('Enter your first name:\n') Enter your first name: Steven >>> print y,x Steven Spielberg

OpenStudy (anonymous):

Thank you both so much.

OpenStudy (anonymous):

That is correct bigcheater. Python actually has both input and raw_input. In python 2.x "raw_input" returns a string of exactly what is input where "input" evaluates what is input and returns it. In python 3.x "input" is like "raw_input" and you can get the old behaviour of "input" by doing "eval(input)". Some 2.x examples below. >>> print raw_input('Enter something:\n') Enter something: 3+4 3+4 >>> print input('Enter something:\n') Enter something: 3+4 7 >>> print eval(raw_input('Enter something:\n')) Enter something: 3+4 7

OpenStudy (anonymous):

Sorry, I'm still a little confused. What is the \n' supposed to denote?

OpenStudy (anonymous):

\n denoted newline, without newline it would be more like Enter something: 3+4 7

OpenStudy (anonymous):

For anyone like me who is looking this answer up way later than it was posted - it's much, much simpler than it appears here! All it really needs to be is: x = raw_input ('Name: ') y = raw_input ('Date of Birth: ') print x, y It's almost like they don't give you a concrete starting place, and figuring it out yourself is a bugger and a half, so after three hours of frustration, this is the answer. Don't complicate it for us newbies, fellas!

OpenStudy (anonymous):

For the code x = raw_input ('Name: ') y = raw_input ('Date of Birth: ')Basically i have to write the first line (x) press enter and write the name, then write the second line (y) press enter and write the date. Then print x,y. Is it possible to write both lines press enter and assign both x and y?

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!