Create a program and store your age in a variable. Then, ask the user for his age and print whether:
- He's older than you
- He's younger than you
- You two have the same age
Pause and return when you finish the exercise.
Solution
Let's start by creating a variable called my_age, get the user's age, and don't forget to convert the user's age to an integer since it's a number, not a text. Using a colon: will make the indentation for us.
There you have the solution for this exercise. But it would be much better if our age was static. If I turn 33 years old, I will have to update the program, which is kind of boring, so we will have to use a date function, which is something we are going to learn later in the course. I hope you like this exercise for now. See you in the next tutorial!
Code:
my_age = 32 user_age = int(input("Type your age: ")) if (user_age > my_age): print("You're older than me") elif (user_age == my_age): print("We are the same age") else: print("You're younger than me")
Output:
Type your age: 28 You're younger than me Type your age: 40 You're older than me Type your age: 32 We are the same age
No comments:
Post a Comment