Answer to Question #264085 in Python for AlessandroM

Question #264085
a) Write a program that reads a string as input from the user. Implement defensive programming so that the string is not empty.
b) Then, print the string after removing the character in position N (1 up to the length of the string) that will be indicated by the user. Implement defensive programming so that the position indicated by the user is valid.
c) Finally, ask the user for a character and return the number and percentage of appearances of that character in the original string.
1
Expert's answer
2021-11-10T11:53:18-0500
s = ''
print('Enter a string other than empty:')
while s == '':
    s = input('>>> ? ')
n = 0
print('Enter the position of the character to be deleted,')
print(f'an integer from {1} to {len(s)}')
while n == 0:
    try:
        n = int(input('>>> ? '))
        if n < 1 or n > len(s):
            print('Incorrect value range please re-enter')
            n = 0
    except ValueError:
        print('Invalid input please try again')
s1 = s[:n-1] + s[n:]
print('Removal result: ',s1)
p = input('Enter character: ')
print('Quantity ',s.count(p))
print('Appearance percentage: ',round(s.count(p)*100/len(s),2),'%')

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS