In [4]:
# this is an import for the pi number
# it is used for your questions below
from math import pi
I will provide the equations in the comments and you must write the code that reflects the equations.
I will also specify which variable you should input. You should write a message indicating what the person is inputting.
I have done the first one for you.
In [5]:
### Area of a Square
# equation: area = side_length ** 2
# input the side length
print("I will calculate the area of a square")
side_length = float(input("Please tell me the side length: "))
area = side_length ** 2
print("The area of a the square with side_length {} is {}".format(side_length, area))
In [6]:
### Area of a Circle
# equation: area = pi * radius**2
# input the radius
In [7]:
### Volume of a Sphere
# equation: area = 4/3 * pi * radius**3
# input the radius
In [8]:
### Weight on Pluto
# explanation:
### if we know how much less gravity a planet has
### then we can calculate our weight on that planet
### pluto has 5% of our gravity, which leads to the equation
# equation: weight_on_pluto = 0.05 * weight_on_earth
# input: your weight on earth
### Bonus
## Given the weight that was input
## Calculate the weights for the other planets.
# Mercury has 38% of our gravity
# Venus has 90%
# The Moon has 16%
# Mars has 38%
# Jupiter has 236%
# Saturn has 108%
# Uranus has 80%
# Nepture has 112%
I will provide a description of a problem. It is your job to convert that description into a formula. Then, you must code that formula into an equation and code.
Each of the problems will require an input that I specify. Write an intro print statement explaining the situation for the problem. Then write an input statement to get the relevant information. Finally, use your equation to calculate the answer. Then, print out the answer in a nice way.
I have completed the first one as an example.
In [9]:
### Luke's Rabbits
# Luke loves his rabbits.
# However, they are breeding like crazy.
# Currently, he has 7 rabbits. Every month, they double. So next month he will have 14.
# Feeding each rabbit costs $10 dollars a month.
#
# The input should be how many months Luke wants to breed his rabbits.
# The output should be how much month it would cost to feed all of those rabbits that month.
print("""Every month, the number of rabbits doubles.
If you tell me how many months you want to breed the rabbits,
then I will calculate how much money it will cost in food""")
num_months = int(input("Number of months: "))
# since it doubles every month, then 7 * 2 is the first month, 7 * 2 * 2 is the second
# so, it is 7 * 2 ** num_months
num_rabbits = 7 * 2 ** num_months
food_cost = 10*num_rabbits
print("It will cost you {} dollars to feed your rabbits that month".format(food_cost))
In [10]:
### Bill's Money
# Bill wants to know how much money he can earn from saving.
# He has an investment account that gives him 10% every month
# He wants to know how much month he will have after 1, 3, and 6 months
#
# The input should be how much money he wants to invest.
# The output should be the numbers for each of the 3 lengths of time.
In [11]:
### Sara's Army
# Sara wants to hire an army.
# It will cost her $500 per soldier.
#
# The input should be the amount of money that Sara has
# The output should be the number of soldiers she can get.
#
# Note that the amount of money she has may not be exactly the amount of a soldier
# For example, if she has $700 dollars, she can only get 1 soldier
# So, you will have to do floor division.
# See the slides if you forgot how
We have been using the following built-in functions.
Practice using them with the following code.
In [3]:
#len
## Calculate the length of the following variables and print the length
x = "Python is a pretty cool language"
y = "What is the length of this string?"
z = """
ASCII ART is pretty cool =D
________
_,.-Y | | Y-._
.-~" || | | | "-.
I" ""=="|" !""! "|"[]""| _____
L__ [] |..------|: _[----I" .-{"-.
I___| ..| l______|l_ [__L]_[I_/r(=}=-P
[L______L_[________]______j~ '-=c_]/=-^
\_I_j.--.\==I|I==_/.--L_]
[_((==)[`-----"](==)j
I--I"~~'''~~"I--I
|[]| |[]|
l__j l__j
|!!| |!!|
|..| |..|
([]) ([])
]--[ ]--[
[_L] [_L]
/|..|\ /|..|\\
`=}--{=' `=}--{='
.-^--r-^-. .-^--r-^-.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"""
print(z)
In [2]:
#print
## use print to make your own ascii art!
# input and int
## use input and ask the user for a whole number between 1 and 10
## convert that number to an int
## multiply one of the above ascii arts by the number and print it
In [ ]:
#### if and only if
# write an if statement that checks if a variable representing temp is higher than 90
# if it is higher than 90, print a message about it being hot
In [ ]:
#### if and an else
# write an if statement that checks if a variable representing temp is lower than 40
# if it is, print a message about it being cold
# if it isn't, (aka, using an 'else'), then print a message about it not being cold
In [ ]:
#### if, elif, and else
# combine the above two
# test if it is above 90 with the if, if it is, print the message from the first part
# test if it is below 40 with an elif, if it is, print the message from the second part
# if both of those ifs are False, then the else will be used
# so, if the else is used, then print out a message about it not being too hot or cold
The following exercises will combine the input function and if statements.
The combination fo these two features of Python is really powerful. I will provide extra directions for each of the exercises. I have also completed the first one.
In [13]:
### A Menu
# You can use input and if statements to create a menu for users
# The only thing that is required is a consistency between what you tell
# your users to input and what you check for with the if statement.
print("Welcome to my menu!")
print("Enter the number of the thing you want to do.")
print("1. See cool ascii art")
print("2. Hear a joke")
print("3. Leave the program")
their_choice = int(input("Please your choice (1,2, or 3): "))
if their_choice == 1:
print(""" Here you go!
; / ,--.
["] ["] ,< |__**|
/[_]\ [~]\/ |// |
] [ OOO /o|__| """)
elif their_choice == 2:
print("Why can't you trust atoms?")
print("Because they make up everything!")
elif their_choice == 3:
print("Okay. Bye!")
else:
print("I have no idea what you type.. but it wasn't 1, 2, or 3")
In [15]:
### Password checker
# Make a log in system
# Ask the user for the password to log in
# If it's the right password, then tell them it was successful!
# If it's not the right password, give them a failure message.
In [16]:
### Personalized Greeter
# Ask the user for their name
# Check if their name is your name
# If it's you, then put a personalized message
# You can use 'elif' and check if it is other names as well
# You can put personalized messages for them too!
# If it's not any of the names you check for, have a default message in the else