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...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers...
Python Create a Python script file called hw3.py. Ex. 1. Write a program that inputs numbers from the user until they enter a 0 and computes the product of all these numbers and outputs it. Hint: use the example from the slides where we compute the sum of a list of numbers, but initialize the variable holding the product to 1 instead of 0. print("Enter n") n = int(input()) min = n while n != 0: if n < min:...
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
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
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,...
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user...
Question: Write a program in python that reads in the file climate_data_2017_numeric.csv and prompts the user to enter the name of a field (other than Date), and then outputs the highest and lowest values recorded in that field for the month of August. The file climate_data_2017_numeric.csv contains the following fields: Date Minimum temperature (C) Maximum temperature (C) Rainfall (mm) Speed of maximum wind gust (km/h) 9am Temperature (C) 9am relative humidity (%) 3pm Temperature (C) 3pm relative humidity (%) Expected...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT