Update some tasks

haskell
Oystein Kristoffer Tveit 2020-09-16 15:01:12 +02:00
parent 23d5a2ef59
commit 67a3e75cee
12 changed files with 129 additions and 132 deletions

View File

@ -1,25 +1,23 @@
def getValues():
def getValues() -> (int, int, int):
while True:
values = input('Gi inn en andregradsliknings a, b og c separert med mellomrom:\n\t')
try:
splitValues = values.split(' ')
assert len(splitValues) == 3
return {
'a': int(splitValues[0]),
'b': int(splitValues[1]),
'c': int(splitValues[2])
}
return map(int, splitValues)
except ValueError:
print('Sørg for at alle tallene er heltall.\n')
except AssertionError:
print('Det skal bare være 3 tall.\n')
values = getValues()
d = values['b']**2 - 4 * values['a'] * values['c']
if __name__ == "__main__":
a, b, c = getValues()
d = b**2 - 4 * a * c
if d > 0:
print('Ligninga har to reelle løsninger')
elif d == 0:
print('Ligninga har en reell løsning')
else:
print('Ligninga har to imaginære løsninger')
if d > 0:
print('Ligninga har to reelle løsninger')
elif d == 0:
print('Ligninga har en reell løsning')
else:
print('Ligninga har to imaginære løsninger')

View File

@ -1,35 +1,23 @@
from math import sqrt
def getValues():
while True:
values = input(
'Gi inn en andregradsliknings a, b og c separert med mellomrom:\n\t')
try:
splitValues = values.split(' ')
assert len(splitValues) == 3
return (
int(splitValues[0]),
int(splitValues[1]),
int(splitValues[2])
)
except ValueError:
print('Sørg for at alle tallene er heltall.\n')
except AssertionError:
print('Det skal bare være 3 tall.\n')
from task11a import getValues
a, b, c = getValues()
d = b**2 - 4 * a * c
if __name__ == "__main__":
a, b, c = getValues()
d = b**2 - 4 * a * c
expression = f'{a}x^2 + {b}x + {c}'
if d > 0:
roots = [(-b + sqrt(d)) / (2 * a), (-b - sqrt(d)) / (2 * a)]
print(
f'Andregradsligningen {expression} har de to reelle løsningene {roots[0]} og {roots[1]}'
)
elif d == 0:
root = (-b + sqrt(d)) / (2 * a)
print(f'Andregradsligningen {expression} har en reell dobbelrot {root}')
else:
print(f'Andregradsligningen {expression} har to imaginære løsninger')
expression = f'{a}x^2 + {b}x + {c}'
if d > 0:
roots = (
(-b + sqrt(d)) / (2 * a),
(-b - sqrt(d)) / (2 * a)
)
print(
f'Andregradsligningen {expression} har de to reelle løsningene {roots[0]} og {roots[1]}'
)
elif d == 0:
root = (-b + sqrt(d)) / (2 * a)
print(f'Andregradsligningen {expression} har en reell dobbelrot {root}')
else:
print(f'Andregradsligningen {expression} har to imaginære løsninger')

View File

@ -1,5 +1,14 @@
def evalPrice(daysToTrip):
return 'Du kan få minipris: 199,-' if (daysToTrip >= 14) else 'For sent for minipris; fullpris 440,-'
try:
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
daysToTrip = int(input('Dager til du skal reise? '))
print(evalPrice(daysToTrip))
def evalPrice(daysToTrip):
return 'Du kan få minipris: 199,-' if (
daysToTrip >= 14) else 'For sent for minipris; fullpris 440,-'
if __name__ == "__main__":
daysToTrip = inputTypeCheck('Dager til du skal reise? ', int)
print(evalPrice(daysToTrip))

View File

@ -1,21 +1,20 @@
try:
from common.inputChecking.boolInput import boolInput
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
def boolInput(question):
while True:
try:
choice = input(question)
assert choice in ['J', 'j', 'N', 'n']
return choice in ['J','j']
except AssertionError:
print('Skriv in J eller N\n')
def miniPriceBranch():
choseMiniPrice = boolInput('Minipris 199,- kan ikke refunderes/endres\nØnskes dette (J/N)? ')
print('Takk for pengene, god reise!' if choseMiniPrice else 'Da tilbyr vi fullpris: 440,-')
daysToTrip = int(input('Dager til du skal reise? '))
if __name__ == "__main__":
daysToTrip = inputTypeCheck('Dager til du skal reise? ', int)
if daysToTrip >= 14:
miniPriceBranch()
else:
print('For sent for minipris; fullpris 440,-')
if daysToTrip >= 14:
miniPriceBranch()
else:
print('For sent for minipris; fullpris 440,-')

View File

@ -1,38 +1,40 @@
try:
from common.inputChecking.boolInput import boolInput
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
def boolInput(question):
while True:
try:
choice = input(question)
assert choice in ['J', 'j', 'N', 'n']
return choice in ['J','j']
except AssertionError:
print('Skriv in J eller N\n')
def miniPriceBranch():
choseMiniPrice = boolInput('Minipris 199,- kan ikke refunderes/endres\nØnskes dette (J/N)? ')
choseMiniPrice = boolInput(
'Minipris 199,- kan ikke refunderes/endres\nØnskes dette (J/N)? ')
if choseMiniPrice:
print('Takk for pengene, god reise!')
else:
normalBranch()
def evalDiscountPercent():
age = int(input('Skriv inn din alder: '))
age = inputTypeCheck('Skriv inn din alder: ', int)
if age < 16:
return 50
elif age >= 60:
return 25
hasSpecialSocialStatus = boolInput('Er du student eller militær (J/N)?')
return 25 if hasSpecialSocialStatus else 0
def normalBranch():
discountPercent = evalDiscountPercent()
print(f'Prisen på biletten blir: {440 - 440 * discountPercent/100}')
daysToTrip = int(input('Dager til du skal reise? '))
if __name__ == "__main__":
daysToTrip = inputTypeCheck('Dager til du skal reise? ', int)
if daysToTrip >= 14:
miniPriceBranch()
else:
normalBranch()
if daysToTrip >= 14:
miniPriceBranch()
else:
normalBranch()

View File

@ -1,3 +1,9 @@
try:
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
INFO = f"""INFO
Dette programmet besvarer om din utleie av egen bolig er skattepliktig.
Først trenger vi å vite hvor stor del av boligen du har leid ut.
@ -9,8 +15,8 @@ HLINE = '----------------------------------------------------------------------'
def mainBranch():
print('DATAINNHENTING:')
percentRented = float(input('Oppgi hvor mye av boligen som ble utleid: '))
rentIncome = float(input('Skriv inn hva du har hatt i leieinntekt: '))
percentRented = inputTypeCheck('Oppgi hvor mye av boligen som ble utleid: ', float)
rentIncome = inputTypeCheck('Skriv inn hva du har hatt i leieinntekt: ', float)
hasTax = percentRented > 50 and rentIncome >= 20000
hasTaxString = 'Inntekten er skattepliktig' if hasTax else 'Inntekten er ikke skattepliktig'
@ -21,10 +27,8 @@ def mainBranch():
if hasTax:
print(f'Skattepliktig beløp er {rentIncome}')
def main():
print(INFO)
print(HLINE)
mainBranch()
if __name__ == "__main__":
main()
print(INFO)
print(HLINE)
mainBranch()

View File

@ -1,3 +1,10 @@
try:
from common.inputChecking.choiceInput import choiceInput
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
INFO = """INFO
Dette programmet besvarer om din utleie en annen type bolig,
her sekundær- eller fritidsbolig, er skattepliktig.
@ -16,32 +23,17 @@ Du har valgt sekundærbolig.
trenger vi først å vite hvor mange sekundærbolig(er) du leier ut.
Deretter trenger vi å vite hvor store utleieinntekter du har pr. sekundærbolig."""
def safeQuestion(question, choices):
while True:
try:
answer = input(question)
assert answer in choices
return answer
except AssertionError:
print('Skriv inn enten', ', '.join(choices[:-1]), 'eller', choices[-1])
def taxEvaluation(housetype, houseAmount, rentPerHouse, hasRentingPurpose=False):
hasTax = True
print()
print(HLINE)
print('SKATTEBEREGNING:')
def fritidsboligBranch():
print(FRITIDSBOLIG_INFO)
print(HLINE)
print('DATAINNHENTING:')
housePurposeIsRenting = safeQuestion(
'Skriv inn formålet med fritidsboligen(e): ',
['Utleie', 'utleie', 'Fritid', 'fritid']
) in ['Utleie', 'utleie']
houseAmount = int(input('Skriv inn antallet fritidsboliger du leier ut: '))
rentPerHouse = int(input('Skriv inn utleieinntekten pr. fritidsbolig: '))
housePurposeIsRenting = choiceInput(
prompt='Skriv inn formålet med fritidsboligen(e): ',
choices=['utleie', 'fritid']
) == 'utleie'
houseAmount = inputTypeCheck('Skriv inn antallet fritidsboliger du leier ut: ', int)
rentPerHouse = inputTypeCheck('Skriv inn utleieinntekten pr. fritidsbolig: ', int)
print()
print(HLINE)
print('SKATTEBEREGNING')
@ -64,23 +56,21 @@ def secondaryHouseBranch():
print(SECONDARYHOUSE_INFO)
print(HLINE)
print('DATAINNHENTING:')
houseAmount = int(input('Skriv inn antallet sekundærboliger du leier ut: '))
rentPerHouse = int(input('Skriv inn utleieinntekten pr. sekundærbolig: '))
houseAmount = inputTypeCheck('Skriv inn antallet sekundærboliger du leier ut: ', int)
rentPerHouse = inputTypeCheck('Skriv inn utleieinntekten pr. sekundærbolig: ', int)
def main():
if __name__ == "__main__":
print(INFO)
print(HLINE)
print('DATAINNHENTING:')
houseType = safeQuestion(
'Skriv inn type annen bolig (sekundærbolig/fritidsbolig) du har leid ut: ',
['Fritidsbolig', 'fritidsbolig', 'Sekundærbolig', 'sekundærbolig']
houseType = choiceInput(
prompt='Skriv inn type annen bolig (sekundærbolig/fritidsbolig) du har leid ut: ',
choices=['fritidsbolig','sekundærbolig']
)
print()
if houseType in ['Fritidsbolig', 'fritidsbolig']:
if houseType == 'fritidsbolig':
fritidsboligBranch()
else:
secondaryHouseBranch()
if __name__ == "__main__":
main()
secondaryHouseBranch()

View File

@ -1,17 +1,22 @@
try:
from common.inputChecking.choiceInput import choiceInput
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
from task9a import mainBranch
from task9b import fritidsboligBranch, secondaryHouseBranch, safeQuestion
from task9b import fritidsboligBranch, secondaryHouseBranch
choices = ['egen bolig', 'sekundærbolig', 'fritidsbolig']
choices.extend([choice.capitalize() for choice in choices])
if __name__ == "__main__":
choices = ['egen bolig', 'sekundærbolig', 'fritidsbolig']
choice = safeQuestion(
question=
'Skriv inn hustype for skatteutregning (egen bolig, sekundærbolig, fritidsbolig): ',
choices=choices)
choice = choiceInput(
prompt=f'Skriv inn hustype for skatteutregning ({", ".join(choices)}): ',
choices=choices)
if choice in ['egen bolig', 'Egen bolig']:
mainBranch()
elif choice in ['sekundærbolig', 'Sekundærbolig']:
secondaryHouseBranch()
else:
fritidsboligBranch()
if choice == 'egen bolig':
mainBranch()
elif choice == 'sekundærbolig':
secondaryHouseBranch()
else:
fritidsboligBranch()

View File

@ -5,7 +5,7 @@ def boolInput(question, error='Skriv in J eller N\n', yesNoLetters=('j', 'n')):
Parameters: \\
prompt (str): The prompt asking the user for input \\
error? (str): The message to be printed on parsing error \\
yesNoLetters? ((str,str)): The letters to be used for representing yes and no in low caps
yesNoLetters? ((str,str)): The letters to be used for representing yes and no in lower caps
"""
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
noLetters = [yesNoLetters[1], yesNoLetters[1].capitalize()]

View File

@ -1,10 +1,12 @@
def safeQuestion(prompt, choices):
def choiceInput(prompt, choices):
"""
Prompts the user to make a choice and asserts that the choice is valid.
Parameters: \\
prompt (str): The prompt asking the user for input \\
choices ([str]): The choices that the user can choose (in low caps)
choices ([str]): The choices that the user can choose (in lower caps)
Returns the choice in lower caps
"""
allChoices = choices + [choice.capitalize() for choice in choices]
while True: