Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The first 5 solutions. #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Solutions/ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Set the values
a = 10
b = 20

# Set a to b, and b to a
a, b = b, a

# The .format replaces all {} respectively with values in the .format
print("a = {}, b = {}".format(
a, # Replace the first {} with the (new) value of a.
b # Replace the second {} with the (new) value of b.
))
23 changes: 23 additions & 0 deletions Solutions/ex10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Get the input from the user
number = int(input("A number: "))

# To get the last number, we divide by ten, round the number down, multiply it by 10 and subtract it from the number variable.
# The division and rounding down happens in one step using the // operator.
to_subtract = 10 * (number // 10)

# Now we subtract them to get the last digit
last_digit = number - to_subtract

# Print it out
# .format() is explained in previous solutions.
print("The last digit of {} is: {}".format(number, last_digit))


# The easiest way to do it is using substrings, which is where you grab a specific part of a string.
# Sadly this wasn't allowed in this exercise.
# If it were, you'd have this as solution:

# number = input("A number: ") # Notice the int() is gone. We do this to get a string, not an integer.
# last_digit = number[-1] # We need the last character of number. -1 is a short operator for that, -2 for the second to last character, etc.
#
# print("The last digit of {} is: {}".format(number, last_digit))
18 changes: 18 additions & 0 deletions Solutions/ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Ask the user for their grades.
geometry = input("Grade for Geometry: ")
algebra = input("Grade for Algebra: ")
physics = input("Grade for Physics: ")

# We need them as numbers, not as strings.
geometry = int(geometry)
algebra = int(algebra)
physics = int(physics)

# Add everything together and divide by 3 to get the average.
average = (geometry + algebra + physics) / 3

# Another possible solution using a list:
# grades = [geometry, algebra, physics]
# average = sum(grades) / len(grades)

print(average)
17 changes: 17 additions & 0 deletions Solutions/ex3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Ask the user for the current value of the BTC, then the percentage of increase/decrease.
bitcoin_value = input("Value of your bitcoin: ")
change_percentage = input("Change% of the BTC: ")

# We need them as numbers, not strings.
bitcoin_value = int(bitcoin_value)
change_percentage = int(change_percentage)

# Calculate by how much the BTC went up or down.
change = (change_percentage / 100) * bitcoin_value

# Print the results.
# The .format is explained in the solution for ex1.
print("New BTC value: {}, it changed with: {}.".format(
bitcoin_value + change,
change
))
9 changes: 9 additions & 0 deletions Solutions/ex4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Get the user input as integers (basically the first two steps from other solutions as one).
width = int(input("Width: "))
height = int(input("Height: "))

# Multiply them together to get the area.
area = width * height

# Print them out
print("Area is: {}.".format(area))
19 changes: 19 additions & 0 deletions Solutions/ex5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Get everything we need from the users in the appropriate types.
money = int(input("How much money do you have: "))
btc = int(input("The value of BTC: "))
eth = int(input("The value of ETH: "))
ltc = int(input("The value of LTC: "))

# See how much we can buy of everything.
# Note the double / to round the division down and keep it an integer.
amount_btc = money // btc
amount_eth = money // eth
amount_ltc = money // ltc

# Let the user know what he can buy and how much.
# Because there is just one value we need in the string it is overkill to use .format()
# Instead we use str() to convert the values to a string, so we can append them to the other strings and print them.
print("You can buy:")
print("Bitcoins: " + str(amount_btc))
print("Ethereum: " + str(amount_eth))
print("Litecoin: " + str(amount_ltc))
9 changes: 9 additions & 0 deletions Solutions/ex6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Get the input from the user in the required types.
laptop_price = int(input("Price of laptop: "))
tax = int(input("Tax%: "))

# Add the tax to laptop_price and store it in the laptop_price variable.
laptop_price = (1 + tax / 100) * laptop_price

# Print it.
print("Laptop with tax: " + str(laptop_price))
18 changes: 18 additions & 0 deletions Solutions/ex7.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Get the input from the user in the required types.
years_worked = int(input("How many years have you worked: "))
children = int(input("How many children do you have: "))

# Salary is:
# 400 minimum wage,
# 20 for every year worked,
# 30 for each child the employee has.
salary = 400 + 20 * years_worked + 30 * children

# Print it.
# Notice the \t character. That stands for a TAB. It makes everything align pretty nicely.
print("Your salary:")
print("Minimum wage:\t\t\t\t400")
print("Years of experience:\t\t" + str(20 * years_worked))
print("Support for your children:\t" + str(30 * children))
print() # This is for an empty newline
print("Total:\t\t\t\t\t\t" + str(salary))
28 changes: 28 additions & 0 deletions Solutions/ex8.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Get the input from the user in the required types.
years_worked = int(input("How many years have you worked: "))
children = int(input("How many children do you have: "))
miss_a_day = input("Did you miss a day of work (y/n): ")

# Validate and parse miss_a_day
# For easy comparison, we make everything entered lowercase.
if miss_a_day.lower() == "y" or miss_a_day.lower() == "yes":
miss_a_day = 0
else:
miss_a_day = 100

# Salary is:
# 400 minimum wage,
# 20 for every year worked,
# 30 for each child the employee has,
# 100 if the employee didn't miss a day of work.
salary = 400 + 20 * years_worked + 30 * children + miss_a_day

# Print it.
# Notice the \t character. That stands for a TAB. It makes everything align pretty nicely.
print("Your salary:")
print("Minimum wage:\t\t\t\t400")
print("Years of experience:\t\t" + str(20 * years_worked))
print("Support for your children:\t" + str(30 * children))
print("All-days-worked bonus:\t\t" + str(miss_a_day))
print() # This is for an empty newline
print("Total:\t\t\t\t\t\t" + str(salary))
26 changes: 26 additions & 0 deletions Solutions/ex9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Ask the user for their grades.
geometry = input("Grade for Geometry: ")
algebra = input("Grade for Algebra: ")
physics = input("Grade for Physics: ")

# We need them as numbers, not as strings.
geometry = int(geometry)
algebra = int(algebra)
physics = int(physics)

# Add everything together and divide by 3 to get the average.
average = (geometry + algebra + physics) / 3

# Another possible solution using a list:
# grades = [geometry, algebra, physics]
# average = sum(grades) / len(grades)

print("Your average: " + str(average))

# Encourage the student to work harder.
if average >= 7:
print("Good job!")
elif 4 < average < 7:
print("You need to work a little harder.")
else:
print("You need to work a lot harder.")
2 changes: 1 addition & 1 deletion exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Create a program that:
* Output: "The total price of the laptop is 330$"

# ex.7
In a company the monthly salary of an employee is calculated by minimum wage 400$ per month, plus 20$ multiply by the employment years, plus 30$ for each employee kid.
In a company the monthly salary of an employee is calculated by minimum wage 400$ per month, plus 20$ multiplied by employment years, plus 30$ for each employee kid.

Create a program that:
* Read the employment years
Expand Down