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.

Expert's answer

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),'%')
LATEST TUTORIALS
APPROVED BY CLIENTS