Python Forum
John Guttag Book - Finger Exercise 3
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
John Guttag Book - Finger Exercise 3
#1
Hi,
Uploading my way of solving the 3rd Finger Exercise from John Guttag's book.

Not as tough as Finger Exercise for 2 though.

#"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)
#
Reply


Messages In This Thread
John Guttag Book - Finger Exercise 3 - by pritesh - Jan-29-2018, 07:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  John Guttag Book Finger Exercise 2. pritesh 6 10,930 Feb-09-2020, 08:08 PM
Last Post: Larz60+
  John Guttag Python Book Finger Exercise 5 pritesh 0 8,270 Mar-04-2018, 09:08 PM
Last Post: pritesh

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020