add inputTypeCheck to common files

This commit is contained in:
Oystein Kristoffer Tveit 2020-08-31 23:16:07 +02:00
parent ee06c0989a
commit db184fe601
11 changed files with 63 additions and 20 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__

View File

@ -1,3 +1,5 @@
from common import inputTypeCheck
class recipe:
def __init__(self, ingredients, standardPortions):
self.ingredients = ingredients

View File

@ -0,0 +1,7 @@
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)

View File

@ -1,5 +1,5 @@
import math
from common import inputTypeCheck
# Bump the decimal point up by numberOfDecimal points,
# add 0.5 to make floor go from 0-1 to 0.5-1.5,
@ -12,14 +12,6 @@ def removeEmptyDecimals(number):
hasEmptyDecimals = (number == int(number))
return int(number) if hasEmptyDecimals else number
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)
number = inputTypeCheck(
message='Gi inn et desimaltall: ',
type=float,

View File

@ -1,12 +1,5 @@
import math
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)
from common import inputTypeCheck
def removeEmptyDecimals(number):
hasEmptyDecimals = (number == int(number))

View File

@ -0,0 +1,7 @@
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)

View File

@ -1,8 +1,18 @@
from common import inputTypeCheck
AVOGADROS_CONSTANT = 6.022e23
substance = input('Si et stoff du er i besittelse av: ')
weight = float(input('Hva er molvekt i gram for vann? '))
amount = float(input('Hvor mange gram vann har du? '))
weight = inputTypeCheck(
message='Hva er molvekt i gram for vann? ',
type=float,
errorMessage='Beklager, det du skrev inn er ikke et heltall. Prøv igjen\n'
)
amount = inputTypeCheck(
message='Hvor mange gram vann har du? ',
type=float,
errorMessage='Beklager, det du skrev inn er ikke et heltall. Prøv igjen\n'
)
numberOfMolecules = (amount * AVOGADROS_CONSTANT / weight)
print(f'Du har {format(numberOfMolecules, ".1e")} molekyler {substance.lower()}')

View File

@ -1,3 +1,5 @@
from common import inputTypeCheck
AMOUNT_OF_POSSIBLE_MELODIES = 8.25e19
melodiesHeard = int(input('Antall ulike 10-toners melodilinjer du har hørt? '))

View File

@ -0,0 +1,7 @@
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)

View File

@ -1,5 +1,13 @@
from math import sqrt
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)
class Tetraeder:
def __init__(self, length):
self.length = length
@ -13,5 +21,12 @@ print(f'Et tetraeder med høyde {figure1.length} har areal {figure1.getArea()}')
print(f'Et tetraeder med høyde {figure1.length} har volum {figure1.getVolume()}')
print()
figure2 = Tetraeder(float(input('Skriv inn en høyde: ')))
figure2 = Tetraeder(
inputTypeCheck(
message='Skriv inn en høyde: ',
type=int,
errorMessage='Beklager, det du skrev inn er ikke et heltall. Prøv igjen\n'
)
)
print(f'Et tetraeder med høyde {figure1.length} har volum {figure2.getVolume()} og areal {figure2.getArea()}')

7
Exercise 1/common.py Normal file
View File

@ -0,0 +1,7 @@
def inputTypeCheck(message, type, errorMessage):
while True:
inputValue = input(message)
try:
return type(inputValue)
except ValueError:
print(errorMessage)