Question

In: Computer Science

Write a Python Program Continue to ask the user to enter a POSITIVE number until they...

Write a Python Program

Continue to ask the user to enter a POSITIVE number until they type DONE to quit.

  • The program should DOUBLE each of the digits in the number and
    display the original entry AND the SUM of the doubled digits.
  • Hint: Use the // and % operators to accomplish this.
  • Print the COUNT of entries that were invalid
  • Examples: if the user enters 93218, the sum of the doubled digits is 18+6+4+2+16 = 46.

User Entry

Output

Explanation

46

Original Number: 46
Sum of Doubled Digits: 20

4*2 + 6*2 = 8 + 12 = 20

93218

Original Number: 93218

Sum of Doubled Digits: 46

9*2 + 3*2 + 2*2 + 1*2 + 8*2 =
18    +    6   + 4    +    2   + 16 = 46

-89

Invalid Entry

567

Original Number: 567
Sum of Doubled Digits: 36

5*2 + 6*2 + 7*2 = 10+12+14 =36

-2

Invalid Entry

DONE

Number of Invalid Entries: 2

Next, continue to ask the user to enter a positive number until they type DONE

  • Display each number in reverse order.
    NOTE: You may NOT use lists and the sort method for this problem!
  • Examples:

User Entry

Output

753

357

8976

6798

2

2

123456

654321

DONE

Program Ended


Solutions

Expert Solution

1 ans:

invalid_count=0

while(True):

n=input("Enter positive Number:")

if(n=="DONE"):

print("Number of invalid Entries:",invalid_count)

break

else:

n=int(n)

if(n<0):

print("Invalid Entry")

invalid_count+=1

else:

n=str(n)

s=0

for i in n:

s=s+int(i)*2

print("Original Number:{}\nSum of Double Digits:{}".format(n,s))

#source code:

#2 ans:

while(True):

n=input("")

if(n=="DONE"):

print("Program Ender")

break

else:

n=int(n)

if(n>0):

n=str(n)

print(n[::-1]) #string slicing

else:

print("Invalid Entry")

#another way

while(True):

n=input("")

if(n=="DONE"):

print("Program Ended")

break

else:

n=int(n)

if(n>0):

n=str(n)

out=""

for i in range(len(n)-1,-1,-1):

out=out+n[i]

print(out)

else:

print("Invalid Entry")


Related Solutions

Write a python program that will ask the user to enter as many positive and negative...
Write a python program that will ask the user to enter as many positive and negative numbers and this process will only stop when the last number entered is -999. Use a while loop for this purpose. Your program should compute three sums; the sum of all numbers, sum of all positive numbers, and the sum of all negative numbers and display them. It should also display the count of all numbers, count of all positive numbers, and count of...
In Java: Write a program that prompts the user to enter a positive number until the...
In Java: Write a program that prompts the user to enter a positive number until the user input a negative number. In the end, the program outputs the sum of all the positive numbers. You should use do-while loop (not while, not for). You cannot use break or if statement in the lab. Hint: if the program adds the last negative number to your sum, you can subtract the last number from the sum after the loop.
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Write, save, and run a Python program Ask the user to enter a number of grams....
Write, save, and run a Python program Ask the user to enter a number of grams. Your program converts that to whole tones, then whole kilograms and whatever is left is in grams. It prints the entered grams , tones , kilograms and the remaining grams.      4 marks for i in range(0,10): print(i) 2    what is wrong in the above python code? display your explanation using print statement. mark 3    insert the following comments into your program of part...
Write a python program which asks the user to enter a positive number that is greater...
Write a python program which asks the user to enter a positive number that is greater than 30 called, “num2” and then does the following: o 1) Print all numbers between 1 and “num2” that are divisible by 2 and 3. o 2) Print all numbers between 1 and “num2” that are either divisible by 6 or 7. o 3) Print all numbers between 1 and “num3” that is not divisible by 5
Write a C++ program that keeps asking user to enter a positive integer number until a...
Write a C++ program that keeps asking user to enter a positive integer number until a sentinel value (999) is entered. Then for each positive number entered by the user, find out how many digits it consists. (Hint: divide a number by 10 will remove one digit from the number. You can count how many divisions it needs to bring the number to 0.) An example of executing such a program is shown below. Note that the user input is...
In python Write the code to ask a user to enter a number in the range...
In python Write the code to ask a user to enter a number in the range of 1-100. Have the code checked to make sure that it is in this range. If it is not, let the user re-enter the number. The user should be able to re-enter numbers until the number is within the correct range.
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The...
USE PYTHON. Create a program that will ask a user to enter whole positive numbers. The program will use a while loop to let the program keep running. The user will be allowed to use the program as long as the quitVariable is equal to 'y' or 'Y'. When the user decides to quit playing, say "Thanks for playing!". The program will use a for loop to test if the number are even or odd. If even print "number is...
Write a Python program to: ask the user to enter two integers: int1 and int2. The...
Write a Python program to: ask the user to enter two integers: int1 and int2. The program uses the exponential operator to calculate and then print the result when int1 is raised to the int2 power. You also want to calculate the result when int1 is raised to the .5 power; however, you realize that it is not possible to take the square root of a negative number. If the value for int1 that is entered is a negative number,...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT