Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write the most functional version of this program. You will compare the age of a given character with the nine companions of the Fellowship
write the most functional version of this program. You will compare the age of a given character with the nine companions of the Fellowship of the Ring. Write a program that compares the age of any character of your choice with the nine companions on quest to destroy the One Ring: the hobbit and ring bearer Frodo Baggins and his gardener Sam Gamgee, the wizard Gandalf, the elf Legolas, the dwarf Gimli, the men Aragorn the ranger and Boromir of Gondor, and the two young hobbits Merry Brandybuck and Pippin Took 1. Ask the user for a name and age of a character. 2. Print out a message displaying the character's age and listing the characters who are older than the given character (print nothing if there are none) 3. Print out a message displaying the character's age and listing the characters who are younger than the given character (print nothing if there are none) Here is the data that you will need: 1. Frodo (51 years old) 2. Samwise (39 years old) 3. Gandalf (2000 years old) 4. Legolas (2931 years old) 5. Gimli (140 years old) 6. Aragorn (88 years old) 7. Boromir (41 years old) 8. Merry (37 years old) 9. Pippin (29 years old) The data structures that you must use for this version are two parallel lists. You should hardcode them as follows: names = ] ages [ "Frodo", "Samwise", "Gandalf", "Legolas", "Gimli", "Aragorn", "Boromir", "Merry", "Pippin", = [51, 39, 2000, 2931, 140, 88, 41, 37, 29] For this version, if the entered age is negative, the program should print "Invalid age." and exit the program. For this version, you can assume that the user will never enter the age of a character that is the same as the age of any of the characters given in the names list.i.e your program does not need to consider the case if age of character entered by user is equal to any of the numbers in the ages list. For this version, you cannot use the dictionaries data structure. For this version, think of a way to generalize your solution. We have nine companions in the Fellowship of the Ring but your code must support any number of companions (5, 10, 100, 10000). This means you can not use hard coded variables and multi-level nested if statements anymore. Hints 1. Think about how a for loop can help you in this version of the program. 2. The name of a character and their corresponding age will always be at the same position in their respective list objects i.e. if Frodo is at position 0 in the names list, then their age 51 will also be at position 0 in the ages list. Think about how you can use the range function and the subscription operation to access the name of the character and their corresponding age. 3. Consider using two list objects (names_older and names_younger) to store the names of characters that are older and younger than the character entered by the user.
Step by Step Solution
★★★★★
3.53 Rating (163 Votes )
There are 3 Steps involved in it
Step: 1
Python code for given question def compareagecharactername characterage names ages namesolder namesyounger for i in rangelennames if agesi characterag...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started