In: Computer Science
Write a Python Program
Continue to ask the user to enter a POSITIVE number until they type DONE to quit.
User Entry |
Output |
Explanation |
46 |
Original Number: 46 |
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 = |
-89 |
Invalid Entry |
|
567 |
Original Number: 567 |
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
User Entry |
Output |
753 |
357 |
8976 |
6798 |
2 |
2 |
123456 |
654321 |
DONE |
Program Ended |
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")