add inputTypeCheck to common files
This commit is contained in:
parent
ee06c0989a
commit
db184fe601
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
__pycache__
|
@ -1,3 +1,5 @@
|
||||
from common import inputTypeCheck
|
||||
|
||||
class recipe:
|
||||
def __init__(self, ingredients, standardPortions):
|
||||
self.ingredients = ingredients
|
||||
|
7
Exercise 1/10 - Bakekurs/common.py
Normal file
7
Exercise 1/10 - Bakekurs/common.py
Normal file
@ -0,0 +1,7 @@
|
||||
def inputTypeCheck(message, type, errorMessage):
|
||||
while True:
|
||||
inputValue = input(message)
|
||||
try:
|
||||
return type(inputValue)
|
||||
except ValueError:
|
||||
print(errorMessage)
|
@ -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,
|
||||
|
@ -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))
|
||||
|
7
Exercise 1/11 - James Bond and Operation Round/common.py
Normal file
7
Exercise 1/11 - James Bond and Operation Round/common.py
Normal file
@ -0,0 +1,7 @@
|
||||
def inputTypeCheck(message, type, errorMessage):
|
||||
while True:
|
||||
inputValue = input(message)
|
||||
try:
|
||||
return type(inputValue)
|
||||
except ValueError:
|
||||
print(errorMessage)
|
@ -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()}')
|
@ -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? '))
|
||||
|
7
Exercise 1/8 - Vitenskapelig notasjon/common.py
Normal file
7
Exercise 1/8 - Vitenskapelig notasjon/common.py
Normal file
@ -0,0 +1,7 @@
|
||||
def inputTypeCheck(message, type, errorMessage):
|
||||
while True:
|
||||
inputValue = input(message)
|
||||
try:
|
||||
return type(inputValue)
|
||||
except ValueError:
|
||||
print(errorMessage)
|
@ -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
7
Exercise 1/common.py
Normal file
@ -0,0 +1,7 @@
|
||||
def inputTypeCheck(message, type, errorMessage):
|
||||
while True:
|
||||
inputValue = input(message)
|
||||
try:
|
||||
return type(inputValue)
|
||||
except ValueError:
|
||||
print(errorMessage)
|
Loading…
Reference in New Issue
Block a user