Commit b178ca21 authored by Swicech's avatar Swicech
Browse files

Uploaded solutions

parent 2ab2a0af
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
amountCookies = int(input("How many cookies do you wish to make? "))
gramsSugar = amountCookies/48 * 400
gramsButter = amountCookies/48 * 320
gramsChocolate = amountCookies/48 * 500
eggs = amountCookies/48 * 2
flour = amountCookies/48 * 460
print("Number of Cookies:", amountCookies, " \nsugar(g):", gramsSugar,
"\nbutter(g):", gramsButter, "\nchocolate(g):", gramsChocolate, "\neggs:", eggs,
"\nflour(g):", flour)
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
cookies1=int(input("How many cookies do you want to make? "))
cookies2=int(input("How many cookies do you want to make now? "))
cookies3=int(input("Lastly, how many cookies do you want to make now? "))
sugar1=cookies1/48*400
sugar2=cookies2/48*400
sugar3=cookies3/48*400
chocolate1=cookies1/48*500
chocolate2=cookies2/48*500
chocolate3=cookies3/48*500
print("Number of cookies: \tsugar(g) \tchocolate(g)" )
print(str(cookies1).ljust(17), str(sugar1).rjust(15), str(chocolate1).rjust(17))
print(str(cookies2).ljust(17), str(sugar2).rjust(15), str(chocolate2).rjust(17))
print(str(cookies3).ljust(17), str(sugar3).rjust(15), str(chocolate3).rjust(17))
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
print('1+2(−3) =', 1+2*(-3))
print('[(3+5·2) +1]:2 =', ((3+5*2)+1)/2)
print('-3^2 + 5*3 - 7 =', -3**2+5*3-7)
#1)
print('5:2-4 =', 5/2 -4)
#2)
print('5·12+6-1 =', 5*12 + 6 - 1)
#3)
print('3(5+2) =', 3*(5+2))
#4)
print('4[(5+3):2 +7] =', 4*((5+3)/2+7))
#5)
print('(−4)^(-3)+5·(3−7:2) =', (-4)**(-3)+5*(3-7/2))
```
%% Output
1+2(−3) = -5
[(3+5·2) +1]:2 = 7.0
-3^2 + 5*3 - 7 = -1
5:2-4 = -1.5
5·12+6-1 = 65
3(5+2) = 21
4[(5+3):2 +7] = 44.0
(−4)^(-3)+5·(3−7:2) = -2.515625
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
print(355, "minutes is", 355 // 60, "hours and", 355 % 60, "minutes.")
print(403, "seconds is", 403 // 60, "minutes and", 403 % 60, "seconds.")
print(67, "days is", 67 // 7, "weeks and", 67 % 7, "days.")
print(100, "hours is", 100 // 24, "days and", 100 % 24, "hours.")
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
import math
# import from math-library
print("|-8|, meaning the absolute value of -8, is", abs(-8))
print(2.544, "rounded to a whole number is", round(2.544))
print("The function int() does not round however, it simply removes the decimals:", int(2.544) )
print(2.544, "rounded to two decimals is", round(2.544, 2))
print("The square root of", 10, "is", math.sqrt(10) )
print("A circle of radius 7 has the circumference", math.tau * 7)
print("A circle of radius 7 has the area", math.pi * 7**2)
```
%% Cell type:markdown id: tags:
a)
%% Cell type:code id: tags:
``` python
float1 = float(input("Input a decimal number: "))
float2 = float(input("Input another decimal number: "))
float3 = float(input("Input your last decimal number: "))
print("Converted to integers gives", int(flyt1),int(flyt2),int(flyt3))
int1 = int(input("Input an integer: "))
print("Converted to a floating point number gives:",float(int1))
```
%% Cell type:markdown id: tags:
b)
%% Cell type:code id: tags:
``` python
name = input("Write your name: ")
age = int(input("Hi, " + name + ", how old are you? "))
ageProgStart = int(input("How old where you when you started programming? "))
progTime = age - ageProgStart
print("You have been programming for", progTime, "years.")
ans = input("Did you find those " + str(progTime) + " years to be rewarding?" )
print("Thank you for your answer," , name)
```
%% Cell type:markdown id: tags:
c)
%% Cell type:code id: tags:
``` python
print("Input a floating point number containing at least 5 digits both before and after.")
numb = float(input("What is your number? ")
int_numb = int(tall)
print("Converted to a whole number using int():", int_numb)
print("Rounded to the closest whole number:", round(numb))
print("Rounded to two decimals:", round(numb, 2))
print("Rounded to four decimals:", round(numb, 4))
print("Rounded to the closest thousand:", round(numb, -3))
print("Integer from int() converted back into a float:", float(int_numb))
```
%% Cell type:markdown id: tags:
d)
%% Cell type:code id: tags:
``` python
float_num = float(input("Write a floating point number: "))
am_deci = int(input("How many decimals do you want? "))
print("The number you gave wasr",float_num, "with",am_deci,
"decimals this becomes", round(float_num, am_deci))
```
%% Cell type:markdown id: tags:
a) and b)
%% Cell type:code id: tags:
``` python
r = 5
print("We have a circle of radius", r)
circumference = 2 * 3.14 * r
print("The circumference is", circumference)
#b) print("The circumference is", format(circumference, '.2f'))
areal = 3.14 * r**2
print("The area is", area)
h = 8
volume = area * h
print("A cylinder with height", h, ": Has a volume, ", volume)
```
%% Output
Vi har en sirkel med radius 5
Omkretsen er 31.400000000000002
Arealet er 78.5
Sylinder med høyde 8 : Volumet er, volum
%% Cell type:markdown id: tags:
c) This is because floating number is not stored accurate in Python.