# Put 1 grain of rice on the first square of a chess board, 2 on the second # 4 on the third, 8 on the fourth, and so forth. Find how many grains of rice # are on the board after you get to n squares # # Matt Bishop, MHI 289I, Winter 2019 # try: n = int(input("Number of squares? ")) except: print("Need a positive integer!") else: # sanity check input if n < 1: print("Your integer must be positive!") else: # good to go! # total = number of grains of rice on board # as n is at least 1, there will be at least # 1 grain of rice total = 1 # x = number of grains of rice on current square x = 1 # now add up the rice grains for i in range(1, n): # going to the next square, so we double the # number of grains of rice on the previous # square to get the number on the current square x = 2 * x # now add it into the total total = total + x # done! print it out print("On a chessboard of", n, "squares, there are", total, "grains of rice")