Exercise -- List and Tuples

 1. Create a program that asks a user for his birthday in the format

"DD-MM-YYYY". Then print:

"You were born in [month]."

Example: "You were born in January."

2. Create a program with a predefined list of people. Ask the user for his name, add it to the end of the list, and print the updated list. 

Goodluck. When you're done, come back to see the solution. And if you don't understand anything, feel free to leave it in the comment section.

Solution to question 1.

Code:

months = (" January", "February", "March", " April", "may", "June", "July", "August", "September", "October", "November", "December")
birthday = input("Type your birthday in the format DD-MM-YYYY:  ")

index = int(birthday[3:5]) - 1
bd_month = months[index]

print("You were born in", bd_month)
Output:
Type your birthday in the format DD-MM-YYYY:  10-04-2003
You were born in  April
>>> 
Solution to Question 2

Code:

people = ["peter", "James", "John"]
user = input("Type your name : ")
people.append(user)
print("Here's the list: ", people)

Output:

Type your name : kelvin
Here's the list:  ['peter', 'James', 'John', 'kelvin']
>>> 

Happy coding. See you the next time.








No comments:

Post a Comment

Python Programming Tutorial 15

 Error Handling Anytime we work with user input, we should make sure not only the data is valid and within the range we want, like what we d...