From 67a3e75cee44484202d1c40d7675893a0c19a18f Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 16 Sep 2020 15:01:12 +0200 Subject: [PATCH] Update some tasks --- Exercise 1/test/__init__.py | 0 Exercise 2/11 - Andregradsligning/task11a.py | 26 +++++---- Exercise 2/11 - Andregradsligning/task11b.py | 48 +++++++---------- .../8 - Billettpriser og rabatter/task8a.py | 17 ++++-- .../8 - Billettpriser og rabatter/task8b.py | 25 +++++---- .../8 - Billettpriser og rabatter/task8c.py | 34 ++++++------ Exercise 2/9 - Skatteetaten/task9a.py | 18 ++++--- Exercise 2/9 - Skatteetaten/task9b.py | 54 ++++++++----------- Exercise 2/9 - Skatteetaten/task9c.py | 31 ++++++----- Exercise 2/test/__init__.py | 0 common/inputChecking/boolInput.py | 2 +- common/inputChecking/choiceInput.py | 6 ++- 12 files changed, 129 insertions(+), 132 deletions(-) delete mode 100644 Exercise 1/test/__init__.py delete mode 100644 Exercise 2/test/__init__.py diff --git a/Exercise 1/test/__init__.py b/Exercise 1/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/Exercise 2/11 - Andregradsligning/task11a.py b/Exercise 2/11 - Andregradsligning/task11a.py index e799665..c76055d 100644 --- a/Exercise 2/11 - Andregradsligning/task11a.py +++ b/Exercise 2/11 - Andregradsligning/task11a.py @@ -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') diff --git a/Exercise 2/11 - Andregradsligning/task11b.py b/Exercise 2/11 - Andregradsligning/task11b.py index 5ca1d6a..11aedc8 100644 --- a/Exercise 2/11 - Andregradsligning/task11b.py +++ b/Exercise 2/11 - Andregradsligning/task11b.py @@ -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') \ No newline at end of file + 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') \ No newline at end of file diff --git a/Exercise 2/8 - Billettpriser og rabatter/task8a.py b/Exercise 2/8 - Billettpriser og rabatter/task8a.py index 142c40e..de4f369 100644 --- a/Exercise 2/8 - Billettpriser og rabatter/task8a.py +++ b/Exercise 2/8 - Billettpriser og rabatter/task8a.py @@ -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)) diff --git a/Exercise 2/8 - Billettpriser og rabatter/task8b.py b/Exercise 2/8 - Billettpriser og rabatter/task8b.py index db30de7..80c2fe5 100644 --- a/Exercise 2/8 - Billettpriser og rabatter/task8b.py +++ b/Exercise 2/8 - Billettpriser og rabatter/task8b.py @@ -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,-') \ No newline at end of file + if daysToTrip >= 14: + miniPriceBranch() + else: + print('For sent for minipris; fullpris 440,-') \ No newline at end of file diff --git a/Exercise 2/8 - Billettpriser og rabatter/task8c.py b/Exercise 2/8 - Billettpriser og rabatter/task8c.py index 7a8b1b6..84e1392 100644 --- a/Exercise 2/8 - Billettpriser og rabatter/task8c.py +++ b/Exercise 2/8 - Billettpriser og rabatter/task8c.py @@ -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() \ No newline at end of file + if daysToTrip >= 14: + miniPriceBranch() + else: + normalBranch() \ No newline at end of file diff --git a/Exercise 2/9 - Skatteetaten/task9a.py b/Exercise 2/9 - Skatteetaten/task9a.py index 762ab39..c1b1823 100644 --- a/Exercise 2/9 - Skatteetaten/task9a.py +++ b/Exercise 2/9 - Skatteetaten/task9a.py @@ -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() \ No newline at end of file + print(INFO) + print(HLINE) + mainBranch() \ No newline at end of file diff --git a/Exercise 2/9 - Skatteetaten/task9b.py b/Exercise 2/9 - Skatteetaten/task9b.py index 08d63be..e5df0fa 100644 --- a/Exercise 2/9 - Skatteetaten/task9b.py +++ b/Exercise 2/9 - Skatteetaten/task9b.py @@ -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. Nå 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() \ No newline at end of file + secondaryHouseBranch() \ No newline at end of file diff --git a/Exercise 2/9 - Skatteetaten/task9c.py b/Exercise 2/9 - Skatteetaten/task9c.py index 2f60590..6ed5b7e 100644 --- a/Exercise 2/9 - Skatteetaten/task9c.py +++ b/Exercise 2/9 - Skatteetaten/task9c.py @@ -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() \ No newline at end of file + if choice == 'egen bolig': + mainBranch() + elif choice == 'sekundærbolig': + secondaryHouseBranch() + else: + fritidsboligBranch() \ No newline at end of file diff --git a/Exercise 2/test/__init__.py b/Exercise 2/test/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/common/inputChecking/boolInput.py b/common/inputChecking/boolInput.py index eddfd61..0f18294 100644 --- a/common/inputChecking/boolInput.py +++ b/common/inputChecking/boolInput.py @@ -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()] diff --git a/common/inputChecking/choiceInput.py b/common/inputChecking/choiceInput.py index 8e78d1b..064ba57 100644 --- a/common/inputChecking/choiceInput.py +++ b/common/inputChecking/choiceInput.py @@ -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: