Jan-29-2018, 07:02 PM
Hi,
Uploading my way of solving the 3rd Finger Exercise from John Guttag's book.
Not as tough as Finger Exercise for 2 though.
Uploading my way of solving the 3rd Finger Exercise from John Guttag's book.
Not as tough as Finger Exercise for 2 though.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
#"Introduction to Computation and Programming Using Python: \ #With Application to Understanding Data (MIT Press) 2nd , Kindle Edition" #by John Guttag # Finger Exercise 3: # Replace the comment in the following code with a while loop # numXs = int(input('How many times should I print the letter X? ')) # toPrint = '' # #concatenate X to toPrint numXs times # print (toPrint) numXs = int ( input ( "How many times should I print the letter X? " ) ) toPrint = '' if numXs < 1 : print ( "Value of numXs is less than or equal to zero" ) else : while numXs > 0 : toPrint = toPrint + "x" numXs = numXs - 1 print (toPrint) #Another way to do it, is convert numXs to absolute value. #numXs = int( (input("How many times should I print the letter X? ") ) ) #toPrint = '' #if numXs == 0: # print ("Are you sure??") #if numXs < 0 : # print ("Value of numXs is less than zero. Converting ", numXs, " to " , abs(numXs) ) # numXs = abs(numXs) # # while numXs > 0: # toPrint = toPrint + "x" # numXs = numXs -1 # print(toPrint) #One more way to do it is to initialize numXs to absolute value. #numXs = abs (int( input("How many times should I print the letter X? ") ) ) #print ("Note: Negative numbers will be converted to absolute value") #toPrint = '' #while numXs > 0: # toPrint = toPrint + "x" # numXs = numXs -1 #print(toPrint) # |