This program will be run interactively using Python; the user should be prompted
ID: 3857282 • Letter: T
Question
This program will be run interactively using Python; the user should be prompted to enter an integer and then your program will produce another integer depending on whether the input was even or odd.
The following is an example of what you might see when you run the program; the input you type is shown in green (press Enter at the end of a line), and the output generated by the program is shown in black text.
Enter an integer: 123
Number is odd, doubling each digit in the integer... Result: 112233
Would you like to enter another integer (y/n): y Enter an integer: 456
Number is even, tripling each digit in the integer... Result: 444555666
Would you like to enter another integer (y/n): y Enter an integer: 124
Number is even, tripling each digit in the integer... Result: 111222444
Would you like to enter another integer (y/n): y Enter an integer: 123
Number is odd, doubling each digit in the integer... Result: 112233
Would you like to enter another integer (y/n): n
When the entered number is odd, you double each digit in the integer, below are a few examples:
When the entered number is even, you triple each digit in the integer, below are a few examples:
Value Result 00
12 111222 124 111222444
You may assume that all of the integers entered (odd or even) will be >= 0.
CS1064 Spring 2017
Also notice that your program should ask whether another integer should be entered. If the response to the prompt is "y" your program should ask for another integer and perform the appropriate computations, if the user inputs an "n" the program should end. At this point you don't have to worry about incorrect input, when asked "Would you like to enter another integer (y/n): ", the input will always be a "y" or an "n" character.
Your program should be able to handle an arbitrary number of integers. In the example above there were only 4 integers processed, but you should be able to handle 10, 20, 50, or more different integers during the course of running your program without issue.
The Mod (%) Operator
The mod operator produces the remainder of an integer division, so 0 % 2 = 0, 1 % 2 = 1, 2 % 2 = 0, and 3 % 2 = 1. One common use of the mod operator is determining whether a number is even or odd. If you mod with 2 and the result is 0, the number is even; if the result is 1, the number is odd:
Further, let's say I want to get each of the digits in the integer in 1234, I can do that with the mod operator and integer division:
# Get the next least significant digit. 123 %10=3
# Shave off one digit using division. 123 // 10 = 12
Your program should able to handle an integer with any number of digits, so you should generalize this example with some sort of loop.
Explanation / Answer
count=0
j=0
num_list=[]
num=input("Enter the Number")
if num % 2 == 0:
for i in range(1,len(str(num))+1):
num_list.append(num%10)
#print num_list
num=num//10
#print num
for j in reversed(num_list):
print str(j)*2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.