From 32ac50e6d7f25e8e6289601644600dbd1bdc7df2 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sat, 3 Mar 2018 21:21:07 +0100 Subject: [PATCH 1/4] Added the solutions for the first 5 exercises --- Solutions/ex1.py | 0 Solutions/ex2.py | 0 Solutions/ex3.py | 0 Solutions/ex4.py | 0 Solutions/ex5.py | 0 5 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Solutions/ex1.py create mode 100644 Solutions/ex2.py create mode 100644 Solutions/ex3.py create mode 100644 Solutions/ex4.py create mode 100644 Solutions/ex5.py diff --git a/Solutions/ex1.py b/Solutions/ex1.py new file mode 100644 index 0000000..e69de29 diff --git a/Solutions/ex2.py b/Solutions/ex2.py new file mode 100644 index 0000000..e69de29 diff --git a/Solutions/ex3.py b/Solutions/ex3.py new file mode 100644 index 0000000..e69de29 diff --git a/Solutions/ex4.py b/Solutions/ex4.py new file mode 100644 index 0000000..e69de29 diff --git a/Solutions/ex5.py b/Solutions/ex5.py new file mode 100644 index 0000000..e69de29 From 6d965843c22f72338d27fd466bbc7ab5c07fedb4 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sat, 3 Mar 2018 21:21:50 +0100 Subject: [PATCH 2/4] And now with the solution --- Solutions/ex1.py | 12 ++++++++++++ Solutions/ex2.py | 18 ++++++++++++++++++ Solutions/ex3.py | 17 +++++++++++++++++ Solutions/ex4.py | 9 +++++++++ Solutions/ex5.py | 19 +++++++++++++++++++ 5 files changed, 75 insertions(+) diff --git a/Solutions/ex1.py b/Solutions/ex1.py index e69de29..14e72eb 100644 --- a/Solutions/ex1.py +++ b/Solutions/ex1.py @@ -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. +)) diff --git a/Solutions/ex2.py b/Solutions/ex2.py index e69de29..c1a31a7 100644 --- a/Solutions/ex2.py +++ b/Solutions/ex2.py @@ -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) diff --git a/Solutions/ex3.py b/Solutions/ex3.py index e69de29..9eff4af 100644 --- a/Solutions/ex3.py +++ b/Solutions/ex3.py @@ -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 +)) diff --git a/Solutions/ex4.py b/Solutions/ex4.py index e69de29..b0f9b26 100644 --- a/Solutions/ex4.py +++ b/Solutions/ex4.py @@ -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)) diff --git a/Solutions/ex5.py b/Solutions/ex5.py index e69de29..a82ae05 100644 --- a/Solutions/ex5.py +++ b/Solutions/ex5.py @@ -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)) From bc09c2a377e614303fa2f59ef3c6df565a24db72 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sun, 4 Mar 2018 11:17:55 +0100 Subject: [PATCH 3/4] Added another 5 exercises. --- Solutions/ex10.py | 23 +++++++++++++++++++++++ Solutions/ex6.py | 9 +++++++++ Solutions/ex7.py | 18 ++++++++++++++++++ Solutions/ex8.py | 28 ++++++++++++++++++++++++++++ Solutions/ex9.py | 26 ++++++++++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 Solutions/ex10.py create mode 100644 Solutions/ex6.py create mode 100644 Solutions/ex7.py create mode 100644 Solutions/ex8.py create mode 100644 Solutions/ex9.py diff --git a/Solutions/ex10.py b/Solutions/ex10.py new file mode 100644 index 0000000..5a32f6f --- /dev/null +++ b/Solutions/ex10.py @@ -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)) diff --git a/Solutions/ex6.py b/Solutions/ex6.py new file mode 100644 index 0000000..ff1ead0 --- /dev/null +++ b/Solutions/ex6.py @@ -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)) diff --git a/Solutions/ex7.py b/Solutions/ex7.py new file mode 100644 index 0000000..fe3a537 --- /dev/null +++ b/Solutions/ex7.py @@ -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)) diff --git a/Solutions/ex8.py b/Solutions/ex8.py new file mode 100644 index 0000000..744e6ed --- /dev/null +++ b/Solutions/ex8.py @@ -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)) diff --git a/Solutions/ex9.py b/Solutions/ex9.py new file mode 100644 index 0000000..b409619 --- /dev/null +++ b/Solutions/ex9.py @@ -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.") From 1c2945b35ed6653aadd8704375bdd86f8c971464 Mon Sep 17 00:00:00 2001 From: Hans Goor Date: Sun, 4 Mar 2018 11:18:11 +0100 Subject: [PATCH 4/4] Spelling/Grammar mistake fixed. --- exercises.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises.md b/exercises.md index 037f6b3..68fa39f 100644 --- a/exercises.md +++ b/exercises.md @@ -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