# Last class review: loop over a string # Ask for a string from the user, and an integer n # Create and print a new string in which each character from # the original string is replicated n times. # Example input: "go!" and n=3, # output print "gggooo!!!" string = raw_input('give me a string: ') number = int(raw_input('give me a number: ')) # without int first result = '' for letter in string: result = result + number*letter # with 3 first print result # tests: # number =0 # number = -1, no crash thus good!! # NEW LOOP: iterations using the range function # range(n) returns a list of numbers # [0, 1, ... n-1] # Ex 1: add successive numbers up to 13 (not included) asum = 0 for num in range(13): asum = asum + num #print asum # shows how asum accumulates the loop variable value print "sum =", asum # Write for loops to print the following sequences of numbers: # A. 11 22 33 44 ... 88 99 for num in range(9): print 11 *(num + 1) # B. -5 -4 -3 -2 -1 0 n = 6 # n can be changed to get longer sequence for num in range(n): print num-(n-1), print # C. 1 2 4 8 16 32 64 256 512 1024 for num in range(11): print 2**num,