Free cookie consent management tool by TermsFeed

Python exercises for Beginners | Exercise #01 - Entering Characters in Python

Python Exercise 1 - Entering Characters in Python


Create a program that asks the user to enter their name and age. Print a message addressed to them informing them of the year they will be 100 years old. Note: For this exercise, you are expected to explicitly write the year.


Tips for this exercise:

Getting user input;

String manipulation;

User Input in Python

To get user input in Python, the command you use is input()
Store the result in a variable and use it to your heart's content. Remember that the result obtained from the user will be a string, even if he enters a number.


For example:

name = input("Give me your name: ")

print("Your name is " + name)


What this will print in the terminal (or in the shell, whichever you're running Python in) will be:


------------------------- -------------------

Give me your name: coder

Your name is coder


What happens at the end of input() is that it waits for the user to type something and press ENTER. Only after the user presses ENTER does the program continue.


Handling strings (a few ways)

What you get from the input() is a string. What can you do with it?

First: Transform the string into a number. Let's say you are 100% sure that the user has entered a number. You can turn the string into an integer with the int(). Here's what it looks like:


age = input("Enter your age: ")

age = int(age)

—-----------------------------------------

Enter your age: 14


The age variable will contain a value that is an integer, and now you can do math with it.

(Note that you can also transform integers into strings in exactly the opposite way, using the str())

Second: Do your math with strings. What I mean by this? I mean, if I want to combine strings (concatenate is the computer science word for it), all I need to do is add them:


print("Lo" + "bo")

print("By" + "ta")

print("6" + "languages​​")

print(str(4) + "phrases")

—--------------------------------------------- --

Lobo

Porta

6linguagens

4frases

The same works for multiplication:

print(4 * "test")

—---------------------------------------------

testtesttesttest


But division and subtraction don't work like that. In terms of multiplication, the idea of ​​multiplying two strings is not well defined. What does it mean to multiply two strings first? However, it makes sense to specify multiplication of a string by a number - just repeat that string that number of times. Try this in your own program with all the arithmetic operations with numbers and strings - the best way to get an idea of ​​what works and what doesn't is to try it!


Exercise Solution - 1

Create a program that asks the user to enter their name and age. Print a message addressed to them informing them of the year they will be 100 years old.


name = input("What's your name: ")

age = int(input("How old are you: "))

year = 2022 - age + 100

print(name + ", you will be 100 years old in the year " + str(year))

—-------------------------------------

What's your name: John

How old you have: 10

John, you will be 100 years old in the year 2112


Post a Comment

0 Comments

Close Menu