I have to find how many characters are in each individual string a user inputs and then total amount of characters from all the inputs, ending the loop when the user enters 'end'.
For some reason my loop won't break, I've tried a different things but nothings working for me. I feel like it's probably something really simple that I'm forgetting, any help would be great!
upper_total = 0
lower_total = 0
alpha_total = 0
num_total = 0
white_total = 0
while True:
user_string = input("Enter a string: ")
for entry in user_string:
if entry == 'end':
break
if entry.isupper():
upper_total += 1
if entry.islower():
lower_total += 1
if entry.isalpha():
alpha_total += 1
if entry.isdigit():
num_total += 1
if entry.isspace():
white_total += 1
print("Capital Letters: ", sum(1 for entry in user_string if entry.isupper()))
print("Lowercase Letters: ", sum(1 for entry in user_string if entry.islower()))
print("Letters: ", sum(1 for entry in user_string if entry.isalpha()))
print("Numbers: ", sum(1 for entry in user_string if entry.isdigit()))
print("White space: ", sum(1 for entry in user_string if entry.isspace()))
print('Upper total: ', upper_total)
print('lower total: ', lower_total)
print('alpha total: ', alpha_total)
print('num total: ', num_total)
print('white space total: ', white_total)
Aucun commentaire:
Enregistrer un commentaire