uaveiro-leci/1ano/1semestre/fp/aula05/telephones.py

43 lines
1.2 KiB
Python
Raw Normal View History

2022-10-20 21:22:14 +00:00
# Convert a telephone number into corresponding name, if on list.
# (If not on list, just return the number itself.)
def telToName(tel, telList, nameList):
# your code here
2023-01-30 16:48:43 +00:00
for index, t in enumerate(telList):
if t == tel:
2023-01-30 16:48:43 +00:00
return nameList[index]
return tel
2022-10-20 21:22:14 +00:00
# Return list of telephone numbers corresponding to names containing partName.
def nameToTels(partName, telList, nameList):
# your code here
tels = []
2023-01-30 16:48:43 +00:00
for index, name in enumerate(nameList):
if partName in name:
2022-10-20 21:22:14 +00:00
tels.append(telList[index])
return tels
2022-10-20 21:22:14 +00:00
def main():
# Lists of telephone numbers and names
telList = ['975318642', '234000111', '777888333', '911911911']
nameList = ['Angelina', 'Brad', 'Claudia', 'Bruna']
2022-10-20 21:22:14 +00:00
# Test telToName:
tel = input("Tel number? ")
print(telToName(tel, telList, nameList))
print(telToName('234000111', telList, nameList) == "Brad")
print(telToName('222333444', telList, nameList) == "222333444")
2022-10-20 21:22:14 +00:00
# Test nameToTels:
name = input("Name? ")
print(nameToTels(name, telList, nameList))
print(nameToTels('Clau', telList, nameList) == ['777888333'])
print(nameToTels('Br', telList, nameList) == ['234000111', '911911911'])
2022-10-20 21:22:14 +00:00
if __name__ == "__main__":
main()