#1.0 #example #This program says "hello" to the world print "Hello World!" #1.1 print "How are you?" #1.2 #-nohashtag-example #NameError: name 'example' is not defined '''I fixed the script by adding '#' before 'example'. This was necessary because python saw 'example' as a possible variable, which in turn was not defined. Since 'example' in this case was not a variable, but a comment, I should make sure python recognizes it as such, by adding the '#' in front of it. ''' #1.3 #print "Hello World #SyntaxError: EOL while scanning string literal '''I fixed the script by adding " after 'World'. This was necessary because python was scanning for the end of the string, which is indicated by ", but before it came accros ", the line ended, hence the error. By adding the " Python knows that the string has ended. ''' #2.0 #2.1 # printing strings in parts using , print "Hello", "dear", "world" # printing strings in parts using + print "Hello" + "dear" + "world" # printing combinations of strings and numbers using , print "Hello" , 8 , 3.5 # printing combinations of strings and numbers using + # This does not work because Python can not add up numbers with strings TypeError: cannot concatenate 'str' and 'int' objects #print "Hello" + 8 + 3.5 # printing combinations of strings and numbers using , and + # this works, but the two numbers are now added up into one print "Hello" , 8 + 3.5 # assigning to variable # this creates a list of string elements Commastring = "Hello", "dear", "world" print Commastring # this creates a string whithout spaces between the elements Plusstring = "Hello" + "dear" + "world" print Plusstring # this creates a list of string and number elements Commastringnumbers = "Hello" , 8 , 3.5 print Commastringnumbers #This does not work because Python can not add up numbers with strings #Plusstringnumbers = "Hello" + 8 + 3.5 #print Plusstringnumbers # this creates a list of one string and one number element (the two numbers are now added up into one) Pluscommastringnumbers = "Hello" , 8 + 3.5 print Pluscommastringnumbers #2.2 # This asks two people for their names, stores the names in variables called name1 and name2 and says hello to both of them. name1 = raw_input ("What is your name? ") name2 = raw_input ("And what is your name? ") print "Hello, " + name1 + " and " + name2 + "! How are you?" #3.0 #3.1 # this prints a multiple line figure, separated from the left hand side with 2 tabs in one line of script print "\t\t| | |\n\t\t @ @ \n\t\t * \n\t\t|\"\"\"|\n" #4.1 # this asks the uses for his/her favo color color = raw_input ("What is your favorite color?") ''' this prints 3 lines: one line with 10 times the color, one line with once the color followed by a space, 8 times the number of characters in the chosen color times a space, and then the color once more and one line with 10 times the color''' print (color+" ") * 10 +"\n"+color+" "+(" "*len(color)+" ")* 8+color+"\n"+(color+" ") * 10
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question