Python'da print ile çıktı alınan değerler bir TXT dosyasına nasıl yazılır?

OnurKrkc

Kilopat
Katılım
19 Ağustos 2018
Mesajlar
319
Merhaba, ben input komutu ile girdiğimiz değer kadar fibonacci sayısı yazdıran bir kod yazdım. Yani input ile bir değer giriyoruz ve o değer kadar fibonacci sayısını bize veriyor. Burada ekrana verdiği değerleri bir TXT dosyasına yazdırmak istiyorum. Bunu nasıl yapabilirim?

Kod:
 #This code returns a fibonacci sequence based on the value you enter.
 value=int(input("Enter the number: "))
 x=0
 y=1
 if value<=0:
 print("Please enter a number greater than", x)
 else:
 #The end command is used to move to the next line.
 k=print(x,y, end=" ")
 for i in range(2,value):
 z=x+y
 print(z, end=" ")
 x=y
 y=z
 
Merhaba, ben input komutu ile girdiğimiz değer kadar fibonacci sayısı yazdıran bir kod yazdım. Yani input ile bir değer giriyoruz ve o değer kadar fibonacci sayısını bize veriyor. Burada ekrana verdiği değerleri bir txt dosyasına yazdırmak istiyorum. Bunu nasıl yapabilirim?

Kod:
 #This code returns a fibonacci sequence based on the value you enter.
 value=int(input("Enter the number: "))
 x=0
 y=1
 if value<=0:
 print("Please enter a number greater than", x)
 else:
 #The end command is used to move to the next line.
 k=print(x,y, end=" ")
 for i in range(2,value):
 z=x+y
 print(z, end=" ")
 x=y
 y=z

Dosya = open("dosya.txt", "W")
Dosya. Write(yazılacak_şeyler)
 
Python:
with open("fibo.txt", "w") as f:
    value = int(input("Enter the number: "))
    x = 0
    y = 1
    if value <= 0:
        print("Please enter a number greater than zero.")
    else:
        f.write(f"{x}\n{y}\n")
    for i in range(2, value):
        z = x + y
        f.write(f"{z}\n")
        x = y
        y = z
 

Bu konuyu görüntüleyen kullanıcılar

Technopat Haberler

Yeni konular

Geri
Yukarı