Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 2: Writing code (20 points) Question 2 requires writing a python progra

ID: 3871264 • Letter: Q

Question

Question 2: Writing code (20 points)

Question 2 requires writing a python program. We recommend you use the IDLE programming environment. For this exercise, please DO NOT use the python Shell window – instead, write all your code in the Editor window, which you can open by

File->newFile

2( a) Counting backwards from 30 by a “digit” specified by the user

This program relates to a memory task that we’ve used in math cognition research (modified slightly for the present activity – usually you would start at 100 not 30 like we do here). The task involves counting backwards by some specified digit from a starting point. So, for instance, we can count backwards from 100 by the number 7, which would produce 93, 86, 79, and so on. Here, we will use a smaller starting number to make things a bit easier, but we will write a flexible program that lets the user decide which number to subtract each time.

What should my program do? We’ve divided the description of the program into part 1 and part 2, just to help do a bit of “divide and conquer” (see week 1 if you don’t recall what this means). You must use iteration - either while or for loops - for this program!

(part 1) The program will ask the user to enter a digit greater than 0 and less than 10 – if they enter a digit that doesn’t meet these conditions, it will continuously ask them to enter a digit until they enter one that is correct (greater than 0 and less than 10). Hint: use a while loop for part 1.

(part 2) Next, the program will begin with the number 30 and count backwards by the digit the user provided in part 1, printing the current number at each iteration, and stopping when a negative number is reached. Your program should not output this negative number, and it should not output the starting point of 30 - see sample output below. Hint: use a while loop or a for loop for part 2 – either will work well here (but make sure you use a loop).

  

Sample output 1, correct digit entered:

Enter a didgit greater than 0 and less than 10: 9

the next digit is : 21

the next digit is: 12

the next digit is: 3

Where did the numbers come from? The number 9 came from the user (code for part 1). Part 2 starts at 30, and continuously subtracts 9 (the number the user provided) – so at first we have 21, then 12, and so on. We are subtracting 9 because that’s what the user told us to do. See the next code sample, where the user asked us to subtract 6 instead.

Sample output 2, incorrect digit entered: Here is sample output that came out when the user initially input two incorrect inputs (12 and then 13) before finally getting a digit in the requested range (they input 6):

Enter a didgit greater than 0 and less than 10: 12

Incorrect entry. Try again

Enter a didgit greater than 0 and less than 10: 13

Incorrect entry. Try again

Enter a didgit greater than 0 and less than 10: 6

the next digit is : 24

the next digit is: 18

the next digit is: 12

the next digit is: 6

Sample output 3, incorrect digit entered:

Here is sample output that came out when the user initially input an incorrect digit (one less than 1) – this time the next value was correct (they input 8):

Enter a didgit greater than 0 and less than 10: -1

INcorrect entry. Try again

Enter a didgit greater than 0 and less than 10: 8

the next digit is: 22

the next digit is: 14

the next digit is: 6

Explanation / Answer

n = int(input("Enter a digit grater than 0 and less than 10:"))
while n <=0 or n >= 10:
     n = int(input("Incorrect entry. Try again :"))
m = 30
while m-n > 0:
      print("The next digit is:",m-n)
      m = m -n

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote