Skip to content
Snippets Groups Projects
Commit abd94faf authored by AB6912's avatar AB6912
Browse files

uudet

parent eb4b4e30
No related branches found
No related tags found
No related merge requests found
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
#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
#"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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment