Exercise(s)
Create a program that asks the user to enter their name and their age. Print out a message addressed to them that tells them the year that they will turn 100 years old.Extras:
- Add on to the previous program by asking the user for another number and printing out that many copies of the previous message. (Hint: order of operations exists in Python)
- Print out that many copies of the previous message on separate lines. (Hint: the string
"\n"is the same as pressing the ENTER button)
Discussion
Concepts for this week:- Getting user input
- Manipulating strings (a few ways)
User input in Python
input(). Store the result in a variable, and use it to your heart's content. Remember that the result you get from the user will be a string, even if they enter a number.For example,
name = input("Give me your name: ")
print("Your name is " + name)
What this will print in the terminal (or the shell, whatever you are running Python in) will be:>>> Give me your name: Michele
Your name is Michele
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.Manipulating strings (a few ways)
input() function is a string. What can you do with it?- Make the string into a number. Let's say you are 100% positive that the user entered a number. You can turn the string into an integer with the function
int(). (In a later exercise or two or three there will be questions about what to do when the user does NOT enter a number and you try to do this; for now don't worry about that problem). Here is what this looks like:
(or, if you want to be more compact with your code)age = input("Enter your age: ") age = int(age)
In both cases,age = int(input("Enter your age: "))agewill hold a variable that is an integer, and now you can do math with it.
(Note, you can also turn integers into strings exactly in the opposite way, using thestr()function) - Doing math with strings. What do I mean by that? I mean, if I want to combine (concatenate is the computer science word for this) strings, all I need to do is add them:
The same works for multiplication, but division and subtraction do not work like this.print("Were" + "wolf") print("Door" + "man") print("4" + "chan") print(str(4) + "chan")
Forgot how to submit an exercise?
Hello Michele,
ReplyDeleteI just got started with these exercises here. Thanks a lot for practicepython :)
Also, could you please tell me if I would get a feedback on my week1 soultion ? Since I am a newbie, it will be great if you could give a feedback.
Thanks a lot
Cheers
Khimya
Hi Khimya!
ReplyDeleteI have not been giving individuals who submitted exercises specific feedback on their code - I chose instead to publish a solution or two from all the submissions that was a good "average" solution for everyone to look at and compare to. If you want to get feedback on your individual solution, feel free to email me your solution (the link to the gist / Github) to my email: michele.pratusevich AT gmail.com.
Michele