Created
February 2, 2018 11:18
-
-
Save JanneSalokoski/b1f58f9192a3ac5e6aae683eb7c61fa6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Luo tähän tiedostoon yksinkertainen komentoriviohjelma, joka toimii laskimena. | |
# Laskimen tarvitsee vähintään pystyä laskemaan x+y -laskuja. | |
# Yritä tehdä laskimesta helppokäyttöinen ja lisätä peruslaskimen ominaisuudet. | |
# Käytä haluamaasi ohjelmointikieltä. | |
# | |
# Koodin ei tarvitse olla täydellinen, eikä sen tarvitse päästä buildaamisesta | |
# läpi. Harjoituksen tarkoituksena on kartoittaa ohjelmointiosaamista. | |
# | |
# python 3 | |
# Eval joka on turvallinen käyttää. Suorittaa vain asioita kuten laskuja, listojen luomista, yms. | |
import literal_eval | |
# TODO Read the docs for more operations and add them | |
help = """Available operations | |
+ addition | |
- substraction | |
/ division (returns a decimal number) | |
// division (division that's rounded to an integer) | |
* multiplication | |
() brackets can be used to change the order of the calculation""" | |
while True: | |
calculation = input('Input a calculation or write help to see available operations. To exit the app type exit\n').lower() | |
if calculation == 'exit': | |
print('Bye bye') | |
break | |
if calculation == 'help': | |
print(help, '\n') | |
continue | |
try: | |
answer = literal_eval(calculation) | |
except: | |
print('Syntax error') | |
continue | |
# If the answer is not a number the operation most likely wasn't a calculation | |
if not isinstance(answer, int) and not isinstance(answer, float): | |
print('Syntax error') | |
else: | |
print(answer, '\n') | |
# Toinen tapa suorittaa tämä olisi voinut olla ottaa kaksi numeroa ja operaatio kerrallaan ja sen avulla tehdä laskin | |
# mutta tällä tavalla saadaan laskimeen eniten ominaisuuksia pienellä vaivalla. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Kirjoita koodi joka käy läpi numerot 1-100. | |
# Kun luku on jaollinen kolmella tulosta "fizz" ja | |
# kun luku on jaollinen viidellä tulosta "buzz" ja | |
# kun luku on jaollinen molemmilla tulosta "fizz buzz" ja | |
# kun luku ei ole jaollinen kummallakaan tulosta itse numero. | |
# (ohjelmointikieli on vapaa) | |
# python 3 | |
for i in range(1, 101): | |
# Katsotaan jaollisuus tarkastamalla että jakojäännös on 0 | |
divisible_by_3 = divmod(i, 3) == 0 | |
divisible_by_5 = divmod(i, 5) == 0 | |
if divisible_by_3: | |
print('fizz') | |
elif divisible_by_5: | |
print('buzz') | |
elif divisible_by_5 and divisible_by_3: | |
print('fizz buzz') | |
else: | |
print(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment