Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
DS4UX (Spring 2016)/Day 3 follow up
(section)
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Get user input with <code>input()</code> === >>> for i in range(100): ... my_input = input("Please type something> ") ... if my_input == "Quit": ... print("Goodbye!") ... break ... else: ... print("You said: " + my_input) ... Please type something> Hello You said: Hello Please type something> How are you? You said: How are you? Please type something> Quit Goodbye! >>> Things to remember about <code>input()</code> *Input() simply asks the user to type something. *You can test out input() interactively. Just go into the python interpreter and type: input("What's your favorite color?") *The stuff that goes inside the parentheses is the "prompt". It's a string, and should be surrounded by quotes. When you run your program, the prompt text will be shown to the user right to the left of the blinking cursor where they will type their input. *Python will ask the user to type something at the point in the script where input() is called. Remember that Python executes scripts from top to bottom, left to right. If you put input inside a loop, it will ask the user to type something every time the loop is executed in your script. *What you DO with that user input is up to you. The best thing to do is to save it as a variable, i.e. user_name = input("Please type your name") *Python saves user input as a string, so if the user types "Daria" in the example above, then user_name will equal "Daria". *Once you've saved your user's input, you can use it like any other string variable. In the case of the babynames challenges, you probably want to compare it with the keys in one of the babynames dictionaries (ssadata.boys or ssadata.girls), so that you can find out how many people share that name. These keys are also strings. *REMEMBER: the keys in the babynames dictionaries are all in lowercase, but you can't necessarily control how a user will type their input--it's natural that people will want to capitalize their own name! Fortunately, there are string methods (https://docs.python.org/3/library/stdtypes.html#string-methods (Links to an external site.)) that will convert any string into all lowercase. You can make a string lowercase by adding .lower() to the end of the string (or the variable that holds the string)!
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information