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

I have written some code for problem set 0. print raw_input('what is your first name?') firstname = raw_input('what is your first name?') print raw_input('what is your last name?') lastname = raw_input('what is your last name?') print ('your name is ' + firstname + lastname) When i run the program why does it ask for my first name twice and my last name twice before displaying the result? >>> what is your first name?Ben Ben what is your first name?Ben what is your last name?Deed Deed what is your last name?Deed your name is BenDeed >>> Also how do i add a space?

OpenStudy (anonymous):

i will check it later i will give u solution

OpenStudy (anonymous):

firstname = raw_input('what is your first name?') actually prints "what is your first name" So, you have two options -- Either have a separate line for the print but DON'T print it again in the raw_input statement (notice that there's a print statement that just prints, then the next line gets raw input and assigns it to firstname): >>> print 'what is your first name?' >>> firstname = raw_input() Or, you can put the text for the print statement in between the parentheses for the raw input and do it all on one line >>> firstname = raw_input('what is your first name?') Your's is printing it all out twice because you're telling it to print out twice (once in the print statement, and then again by sending the string as a parameter to raw_input when you assign the variable).

OpenStudy (anonymous):

for the space problem try this print ('your name is ' + str(firstname) + ' ' + str(lastname)) you will get a space in between,, i guess you can remove the str also

OpenStudy (anonymous):

Ok great, thanks for the help. so i guess all i need is: firstname = raw_input('what is your first name?') lastname = raw_input('what is your last name?') print ('your name is ' + firstname + lastname) it has confused me slightly as the raw_input is printing the string even though i never instructed it to.

OpenStudy (anonymous):

raw_input() asks the user for a string of data (ended with a newline), and simply returns the string - http://en.wikibooks.org/wiki/Python_Programming/Input_and_output So the raw_input function has kind of a print function built in to it.

OpenStudy (anonymous):

@Bwaters Yes, yes it does :) Also, like @Aleem said, you might want to put a space in between the first and last names in your final print statement (just so they don't run together). to do this you just put a plus sign, two single quotes with a space between them, and then another plus sign + ' ' + in between first and last name in your print statement, thusly: print ('your name is ' + firstname + ' ' + lastname) That way you get: your first name is John Smith instead of: your first name is JohnSmith

OpenStudy (lopus):

http://dpaste.com/788502/ execute written problem0()

OpenStudy (lopus):

print ('your name is ' + firstname +' '+ lastname)

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!