From abd94faf8ced5b66999308c78049804cbecc38a4 Mon Sep 17 00:00:00 2001 From: Sampsa <AB6912@student.jamk.fi> Date: Tue, 23 Nov 2021 18:52:44 +0200 Subject: [PATCH] uudet --- "L14 Poikkeusten k\303\244sittely/L14T02.py" | 14 ++++++++++---- "L14 Poikkeusten k\303\244sittely/L14T03.py" | 17 +++++++++++------ "L14 Poikkeusten k\303\244sittely/L14T04.py" | 17 +++++++++++++---- L15 Tiedostot/L15T03.py | 3 ++- 4 files changed, 36 insertions(+), 15 deletions(-) diff --git "a/L14 Poikkeusten k\303\244sittely/L14T02.py" "b/L14 Poikkeusten k\303\244sittely/L14T02.py" index c85fe62..e86d68c 100644 --- "a/L14 Poikkeusten k\303\244sittely/L14T02.py" +++ "b/L14 Poikkeusten k\303\244sittely/L14T02.py" @@ -1,5 +1,11 @@ -f = open("c:", "r") -lines = f.readlines() -f.close() -print(lines) +#Tulokseksi tuli PermissionError, koska c:n juureen ei voi tehdä tekstitiedostoja. +import os +path = "c:/" +files = os.listdir(path) + +for f in files: + print(f) +filename = "c:/ayho.txt" +file = open(filename, "w") +file.close() \ No newline at end of file diff --git "a/L14 Poikkeusten k\303\244sittely/L14T03.py" "b/L14 Poikkeusten k\303\244sittely/L14T03.py" index 60e6599..2b26b4f 100644 --- "a/L14 Poikkeusten k\303\244sittely/L14T03.py" +++ "b/L14 Poikkeusten k\303\244sittely/L14T03.py" @@ -1,12 +1,17 @@ +#Kysymyksen asettelu on hiukan epäselvä mutta mutta ohjelman voi suorittaa loppuun lisäämällä try ja except. + def isthiszero(num): - myNum = int(num) - if myNum == 0: - return True - elif myNum != 0: - return False + try: + myNum = int(num) + if myNum == 0: + return True + elif myNum != 0: + return False + except ValueError: + print("Et syöttänyt lukua!") + num = input("Anna joku luku: ") result = isthiszero(num) -print(num) print(result) \ No newline at end of file diff --git "a/L14 Poikkeusten k\303\244sittely/L14T04.py" "b/L14 Poikkeusten k\303\244sittely/L14T04.py" index 6b7128c..31f3f3a 100644 --- "a/L14 Poikkeusten k\303\244sittely/L14T04.py" +++ "b/L14 Poikkeusten k\303\244sittely/L14T04.py" @@ -1,5 +1,14 @@ +#"Korjaa ohjelma niin ettei se kaadu, jos käyttäjä syöttää indeksin, joka on listan ulkopuolella." +#Ohjelma ei kaadu, vaan teksti menee listan perälle, koska se on kuitenkin ennen haluttua indeksiä. + textList = ["Teksti1", "Teksti2", "Teksti3", "Teksti4", "Teksti5"] -index = int(input("Mihin kohtaa listaa haluat syöttää tekstin? ")) -newText = (input("Syötä teksti: ")) -textList.insert(index, newText) -print(textList) \ No newline at end of file +length = len(textList) +try: + index = int(input(f"Listassa on {length} tekstiä. Mihin listan indeksiin 0-{length} haluat syöttää tekstin? ")) + newText = (input("Syötä teksti: ")) + textList.insert(index, newText) + print(f"Teksti sijoitettiin kohtaan {(textList.index(textList[-1]))}, koska antamasi indeksi on listan ulkopuolella.") + for i in textList: + print(i) +except: + print("Antamasi syöte ei ollut kelvollinen indeksi, yritä uudelleen.") \ No newline at end of file diff --git a/L15 Tiedostot/L15T03.py b/L15 Tiedostot/L15T03.py index 67334f9..f20519c 100644 --- a/L15 Tiedostot/L15T03.py +++ b/L15 Tiedostot/L15T03.py @@ -14,5 +14,6 @@ for x in names: nameslist = [] print("Nimiä on", len(namedici)) print("Esiintymät: ") -for x in namedici: +dump = sorted(namedici) +for x in dump: print(x, "esiintyy", namedici[x], "kertaa.") \ No newline at end of file -- GitLab