Last active
March 2, 2017 13:30
-
-
Save joetechem/00ca9dbae6a381de13120e5b9def3ff8 to your computer and use it in GitHub Desktop.
a few exercises to learn how to write interactive programs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Python 2.7 | |
# code examples and explanations from Python Crash Course by Eric Matthes | |
#### | |
#### | |
# echo.py | |
# A simple program to have the computer take input from the user... Or so it seems! | |
message = ("Tell me something, and I will repeat it back to you: ") | |
print(message) | |
# Now, the message is printed, but the user cannot type anyhting in! | |
# What function do we need to add and where? | |
#### | |
#### | |
# greetings.py | |
# So, The input() function, or raw_input() takes one argument: the prompt | |
# or instructions, that we want to display to the user so they know what to do. | |
# Argument = a piece of information passed from a function call to a function | |
# NOTE: Though this is originally written in 2.7, I will refer to raw_input() as input() | |
# IMPORTNANT: WRITE CLEAR PROMPTS! | |
# Each time you use the input() function, you should include a clear, easy-to-follow | |
# prompt that tells the user exactly what kind of inforamtion you're looking for. | |
# Write a program that asks for the user's name, then have the | |
# exact name they typed in return to the user. | |
################################################## | |
################################################## | |
####name = raw_input("Please enter your name: ") | |
####print("Hello, " + name + "!") | |
# Sometimes you'll want to write a longer prompt, perhaps more than one line | |
# For example, you might want to tell the user why you're asking for certain input | |
# You can store your prompt in a variable and pass that variable to the input() function | |
# This allows you to build your prompt over several lines, | |
# then write a clean input() statement | |
# Remember, think of a statement as a complete thought | |
prompt = "If you tell us who you are, we can create a unique message for you to see." | |
prompt += "\nWhat is your first name? " | |
# the operator += adds the new string to the end of the sting value from the line above | |
# what do we need to put on this line to actually take the input from the user?! | |
print("\nHello, " + name + "!") | |
# This example shows another way to build a multi-line string. | |
# The first line stores the first part of the message in the variable, prompt | |
# In the next line, the operator, += takes the string that was stored in prompt | |
# and adds the new string onto the end | |
# As you can see, the prompt now spans two lines, with a space after the question mark for clarity. | |
#### | |
#### | |
# age.py | |
# Making comparisions with user input | |
# Python 2.7 | |
# When you use the input() function, Python interprets everything the user enters | |
# as a string. Consider the next interpreter session, where the computer asks for the user's age: | |
age = raw_input("How old are you? ") | |
print(age) | |
# This seems fine at first, but Python returns the string representation | |
# of the number entered by the user | |
# We can fix this by using the int() function, which tells Python to treat | |
# the input as a numerical value. | |
# The int() function converts a string representation of a number to a numerical representation | |
# FIGURE OUT WHERE TO PUT THE int() FUNCTION!!! | |
age = raw_input("How old are you? ") | |
age = (age) # Hint: change this line to age = int(age) to convert the string to a whole number | |
print(age >= 18) # if you try this in the interpreter, omit the print() function | |
#### | |
#### | |
# theme_park.py | |
# Let's use the int() function in another program | |
# Imagine you are ready to ride some big roller coasters at a theme park! | |
# Have you ever had your height checked, to see if you are tall enough to ride a certain rollercoaster? | |
# Write a program that determines whether a person is tall enough | |
# to ride a certain roller coaster: | |
height = raw_input("How tall are you, in inches? ") | |
height = int(height) | |
# NOW, PUT THE CONDITION STATMENTS BELOW! | |
# To help you get started... | |
if height |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment