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
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,...
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
Write a python program that asks the user to enter a string containing numbers separated by...
Write a python program that asks the user to enter a string containing numbers separated by commas, e.g., s = '1.23,2.4,3.123', Your program should then calculate and print the sum of the numbers entered. Hint: you need to iterate over the string searching for the commas, i.e. their index. The first number is obtained by slicing between the start of the string and the index of the first comma. The second number is between the last comma and the next...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file...
Problem: Write a Python module (a text file containing valid Python code) named p5.py. This file must satisfy the following. Define a function named rinsert. This function will accept two arguments, the first a list of items to be sorted and the second an integer value in the range 0 to the length of the list, minus 1. This function shall insert the element corresponding to the second parameter into the presumably sorted list from position 0 to one less...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string...
Write a program, Lab02_Q4.py, that inputs a string from the user, and creates a new string that deletes each non-alphanumeric character in the original string. You should solve this problem in 2 ways, first use a for loop that iterates through each character in the string, the second should use a while loop that iterates through a range. Keep spaces in the new string also. Hint: You can invoke the isalnum() function on a character, and it will return True...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT