Question

In: Computer Science

Write a Python program stored in a file q3.py that: Gets single-digit numbers from the user...

Write a Python program stored in a file q3.py that:

  • Gets single-digit numbers from the user on one line (digits are separated by white space) and adds them to a list. The first digit and the last digit should not be a zero. If the user provides an invalid entry, the program should prompt for a new value.

  • Converts every entry in the list to an integer and prints the list. The digits in the list represent a non-negative integer. For example, if the list is [4,5,9,1], the entries represent the integer 4591. The most significant digit is at the zeroth index of the list.

  • Adds the integer that the reversed value of the integer. For example, if the list is [4,5,9,1], the addition will be 4591 + 1954 = 6545 and the resulting list will be [6,5,4,5]

Sample Output#1:

  1. **************************************************

  2. Enter single digits separated by one space: 0 5 1 1

  3. Invalid list, try again.

  4. Enter single digits separated by one space: 8 5 9 0

  5. Invalid list, try again.

  6. Enter single digits separated by one space: 0 58 1 14

  7. Invalid list, try again.

  8. Enter single digits separated by one space: 4 5 9 1

  9. The initial list is: [4,5,9,1]

  10. The reversed list is: [1,9,5,4]

  11. The addition list is: [6,5,4,5]

  12. **************************************************

Solutions

Expert Solution

Solution:

Look at the code and comments for better understanding........

Screenshot of the code:

Output:

Code to copy:

while True:
        print("Enter single digits separated by one space: ",end="")
        L = input().split()
        m_digit = False
        #convert the string digits into numbers and check for multi digit numbers
        for i in range(len(L)):
                L[i] = int(L[i])
                if L[i] > 9:
                        m_digit = True
        #if zero is present at begining or end or multidigit number is present
        if L[0]==0 or L[-1]==0 or m_digit:
                print("Invalid list, try again.")
                continue
        #reversing the list L
        rev_L = L[::-1]
        #calculating numbers from the lists
        N,rev_N = 0,0
        for i in range(len(L)):
                N = N*10 + L[i] 
                rev_N = rev_N*10 + rev_L[i]
                #calculating the sum
        SUM = N + rev_N
        #converting the SUM to a list
        SUM_L = []
        while SUM!=0:
                rem = SUM % 10
                SUM_L = [rem] + SUM_L
                SUM//=10
        #printing the results
        print("The initial list is:",L)
        print("The reversed list is:",rev_L)
        print("The addition list is:",SUM_L)
        break

I hope this would help...........................:-))


Related Solutions

Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
write a program that gets four digits from the user and tests to see which digit...
write a program that gets four digits from the user and tests to see which digit is the biggest and which is the smallest, use single-alternative only sample output is below: enter four digits and i will tell you which is biggest and smallest: 4 6 7 9 the biggest is 9 the smallest is 4
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a...
Write a program IN PYTHON of the JUPYTER NOOTBOOK Write a Python program that gets a numeric grade (on a scale of 0-100) from the user and convert it to a letter grade based on the following table. A: 90% - 100% B 80% - 89% C 70% - 79% D 60% - 69% F <60% The program should be written so that if the user entered either a non-numeric input or a numeric input out of the 0-100 range,...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. Example: Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in...
for python 3 a. Ask the user to enter 10 numbers. Each number is stored in a list called myList. Compute and print out the sum and average of the items in the list. Print the numbers that are divisible by 2 from myList. b. Instead of using a list to store the numbers, create a loop to accept the numbers from the user, and find the average of all the numbers. Hint: use an accumulator to store the numbers...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the...
"PYTHON" Write some code " USING PYTHON" to keep reading numbers from the user until the users enters a negative number. The program then prints out: a) the sum of all the numbers b) the average of all the numbers c) the max of the numbers d) the min of the numbers Note we did not cover lists (array) so you will not use them in this problem. Finally, ask the user for their name, then print their name as...
Write a Python program that reads a file, input by the user, containing one word/token per...
Write a Python program that reads a file, input by the user, containing one word/token per line with an empty line between sentences. The program prints out the longest word found in the file along with its length.
Write a program that will print the whole numbers from a user-specified minimum to a user-specified...
Write a program that will print the whole numbers from a user-specified minimum to a user-specified maximum. Display the total amount of numbers printed.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT