|
| 1 | +#22/12/23 |
| 2 | +#This program stores user inputed names in a list and sorts them alphabetically |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +#Accepts user input of how many names to be stored |
| 7 | +number_of_names = int(input('Enter no of total names to input: ')) |
| 8 | + |
| 9 | +#Declaration of empty list to store each name after each iteration |
| 10 | +allnames_list = [] |
| 11 | + |
| 12 | + |
| 13 | +#Loop to request user input, range is dependent on how many names user wishes to store |
| 14 | +for i in range(number_of_names): |
| 15 | + #Accepts user input |
| 16 | + firstname = input('Enter first name: ').capitalize() #.capitalize changes first letter in word to Caps |
| 17 | + lastname = input('Enter last name: ').capitalize() |
| 18 | + |
| 19 | + print(f'Stored') #Just to alert user that name has beeen saved |
| 20 | + fullname = f"{firstname} {lastname}" #f string that joins(concatenates) firstname and lastname together into new variable fullname |
| 21 | + |
| 22 | + #adds each fullname to already declared list |
| 23 | + allnames_list.append(fullname) |
| 24 | + |
| 25 | + |
| 26 | +#sorts out the list alphabetically |
| 27 | +allnames_list.sort() |
| 28 | + |
| 29 | +#prints the output |
| 30 | +print(allnames_list) |
| 31 | + |
| 32 | + |
| 33 | +#Challenges |
| 34 | +#1. List was storing only the name of the last loop |
| 35 | + |
| 36 | +# I was able to solve that by checking help from chatgpt |
| 37 | +# i just had to simply use the append method to add every outcome to the list aftr each iteration. |
| 38 | + |
| 39 | +#2. Making the letter of the firstname capital |
| 40 | + |
| 41 | +# I forgot to add () to the .upper method. Very silly mistake |
| 42 | +# well it didn't work instead firstname was returning only the first capital letter of the user's input' |
| 43 | + |
| 44 | +# I later solved this by using .capitalize() as that is it's specific function |
| 45 | +#.upper makes the whole word in caps while .title()- makes the first letter of every word present in capital |
| 46 | + |
| 47 | +#3. Arranging alphabetically by first name |
| 48 | +# I actually thought this would be an issue but i was surprised python already had a .sort method for that |
| 49 | +# .sort() - sorts the list while .sorted()- sorts the list but in a new list |
0 commit comments