Question: Approach to Programming Assignment No. 5 DSCI 15310 Computational Thinking and Programming 1 Approach to Programming Assignment No. 5 DSCI 15310 Computational Thinking and Programming

Approach to Programming Assignment No. 5 DSCI 15310 Computational Thinking and Programming 1 Approach to Programming Assignment No. 5 DSCI 15310 Computational Thinking and Programming Approach to Programming Assignment 5 In this program we are going to ask the user to provide us with a lower and upper range of either four- or five-digit numbers to determine which ones are palindromes. We will tell the user to provide one of them, i.e., a four- or five-digit number (we wont mention the other). We will select a four-digit range of numbers. Any text in green and in square brackets [ ] is an explanation We begin by telling the user what this program will do. print ("This program will determine how many four-digit palindromes there are in a given range.") [We then prompt the user to give us the lower and upper numbers of the range. Notice that the inputs are immediately converted to integers, because in the while expression we have to compare the numbers. Strings wont work here.] first = int(input(" Enter the first four-digit number in the range. ")) last = int(input(" Enter the last four-digit number in the range. ")) [In order to know how many palindromes there are within the given range, we have to count them from the first one onwards until we reach the end of the range. To do this, we declare a variable called counter, but you can, of course, use other names for this variable. We initialize the variable with the number 0.] counter = 0 [The simplest way to tackle this problem is to set up a while loop. With each iteration of the while loop, the lower range number will increase by one until it equals the last number in the range. We write this as ] while first <= last: [Remember that the while loop ends in a colon.] first = str(first) [After the comparison, the number returns to being a string, because we can only index numbers when they are strings.] if first[0] == first[3] and first[1] == first[2]: [This expression determines whether the number is a palindrome.] Approach to Programming Assignment No. 5 DSCI 15310 Computational Thinking and Programming 2 counter = counter + 1 [If the number is a palindrome, we increment the counter by one.] print (first) [If the number is a palindrome, we print it.] first = int(first) [We now go to the next number in the range by converting the variable into an integer.] first = first + 1 [We increment the number by one and begin the loop anew.] [Notice that the if expression does not require an else in this case theres no need for it here.] [When the while loop is completed, we tell the user how many palindromes there were in the range ] print ("There are", counter, "four-digit palindromes.") input ("Press enter to exit.") [This concludes your IDLE interaction.] Remember that you can use your own setup. What would you do if you asked the user to give you two five-digit numbers? You would express your if expression in the following way if first[0] == first[4] and first[1] == first3]: The third digit in the number would be first[2]. Since it never changes, we dont need to put it in our if expression. How would you set up your program if you had to ask the user whether s/he wanted a four-digit or a five-digit number? You would probably use an if/else expression. What if you gave a prompt to supply either a four- or five-digit number? Regarding the latter, you would have to use the length function len(first). You might want to test to see whether both the first and last numbers in the range are the same length. This would be a good follow-up programming problem.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!