Difference between revisions of "CSC111 Print Exercises"

From dftwiki3
Jump to: navigation, search
(Figuring out how to print variable number of characters)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
=Python Version 3=
 
=Python Version 3=
  
 +
==Understanding the Output==
 
<source lang="python">
 
<source lang="python">
  
Line 53: Line 54:
 
     x = input( ": " )
 
     x = input( ": " )
 
     y = eval( input( ": " ) )
 
     y = eval( input( ": " ) )
     print "x*y =",  x * y
+
     print( "x*y =",  x * y )
 
      
 
      
 
main()
 
main()
Line 59: Line 60:
 
</source>
 
</source>
  
 +
==Figuring out how to print variable number of characters==
 +
 +
* Write a program that outputs this:
 +
 +
  *
 +
  **
 +
  ***
 +
  ****
 +
  *****
 +
  ******
 +
  *******
 +
 +
* Same question, but now this way:
 +
 +
  *******
 +
  ******
 +
  *****
 +
  ****
 +
  ***
 +
  **
 +
  *
 +
 +
* Or this way?
 +
      *
 +
      **
 +
    ***
 +
    ****
 +
  *****
 +
  ******
 +
*******
 +
 +
* Or, finally, like this?
 +
 +
*******
 +
  ******
 +
  *****
 +
    ****
 +
    ***
 +
      **
 +
      *
  
 
=Python Version 2=
 
=Python Version 2=

Latest revision as of 09:07, 19 September 2011

Python Version 3

Understanding the Output

# D. Thiebaut
# Predict the output of the program below 

def main():
    a = 3
    degrees = 29
    pi = 3.14159
    name = "Julie"
    town = "Northampton"
    
    print( a, pi, name, town )

    input( ">" )

    print( "the temperature in", town, "is", degrees, "degrees" )
    
    input( ">" )

    print( "I love living in", town, "!" )

    input( ">" )

    print( "The temperature in ", town, "is", degrees, "!" )

    input( ">" )
    
    print( "the temperature in, say,", town, "isn't", degrees )

    raw_input( ">" )

    print( '"',",","',',","''",'",',",,'" )

    input( ">" )

    print( '"'+"+"+"'+',"+"+'"+'"-,'+"+,'" )

    input( ">" )

    print( '"'+"+"+"'+',"+"+'"+'"-,'+"+,'"*3 )

    input( ">" )

    x = input( "Enter a number between 0 and 10: " )
    y = input( "Enter another number between 0 and 10: " )
    print "x + y =", x+y

    input( ">" )

    x = input( ": " )
    y = eval( input( ": " ) )
    print( "x*y =",  x * y )
    
main()

Figuring out how to print variable number of characters

  • Write a program that outputs this:
 *
 **
 ***
 ****
 *****
 ******
 *******
  • Same question, but now this way:
 *******
 ******
 *****
 ****
 ***
 **
 *
  • Or this way?
      *
     **
    ***
   ****
  *****
 ******
*******
  • Or, finally, like this?
*******
 ******
  *****
   ****
    ***
     **
      *

Python Version 2

# D. Thiebaut
# Predict the output of the program below 

def main():
    a = 3
    degrees = 29
    pi = 3.14159
    name = "Julie"
    town = "Northampton"
    
    print a, pi, name, town

    raw_input( ">" )

    print "the temperature in", town, "is", degrees, "degrees"
    
    raw_input( ">" )

    print "I love living in", town, "!"

    raw_input( ">" )

    print "The temperature in ", town, "is", degrees, "!"

    raw_input( ">" )
    
    print "the temperature in, say,", town, "isn't", degrees

    raw_input( ">" )

    print '"',",","',',","''",'",',",,'"

    raw_input( ">" )

    print '"'+"+"+"'+',"+"+'"+'"-,'+"+,'"

    raw_input( ">" )

    print '"'+"+"+"'+',"+"+'"+'"-,'+"+,'"*3

    x = raw_input( "Enter a number between 0 and 10: " )
    y = raw_input( "Enter another number between 0 and 10: " )
    print "x + y =", x+y

    raw_input( ">" )

    x = raw_input( ": " )
    y = input( ": " )
    print "x*y =",  x * y
    
main()