N butun son berilganu ning darajasi ekanligini aniqlang


orasidagi “egizak tub” sonlarni “output.txt” fayliga yozsin


Download 134.94 Kb.
bet9/10
Sana22.06.2023
Hajmi134.94 Kb.
#1647477
1   2   3   4   5   6   7   8   9   10
Bog'liq
dasturlash shpargalka

orasidagi “egizak tub” sonlarni “output.txt” fayliga yozsin.
Dasturda “egizak tub” sonlar bo’lmasa “output.txt” fayliga
egizak tub son yo’q deb yozilsin. Dasturni yozishda
try...except dan foydlaning
import random
import math
def is_prime(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
try:
with open("input.txt", "w") as input_file:
primes = []
for _ in range(50):
number = random.randint(10, 1000)
input_file.write(str(number) + "\n")
if is_prime(number):
primes.append(number)
with open("output.txt", "w") as output_file:
if primes:
for prime in primes:
output_file.write(str(prime) + "\n")
else:
output_file.write("egizak tub son yo'q")
except:
with open("output.txt", "w") as output_file:
output_file.write("Fayl yozishda xatolik yuz berdi")


38) “input.txt” fayliga tasodifiy ravishda 50 ta [10, 1000]
orasidagi sonlarni yozing va shu sonlarni o’qib raqamlari
yig’indisi 10 ga teng bo’lgan sonlarni “output.txt” fayliga
yozsin. Dasturda bunday sonlar bo’lmasa “output.txt”
fayliga “Bunday son yo’q” yozuvi yozilsin. Dasturni yozishda
try ... except dan foydalaning
import random
try:
with open("input.txt", "w") as input_file:
numbers = []
for _ in range(50):
number = random.randint(10, 1000)
input_file.write(str(number) + "\n")
numbers.append(number)
with open("output.txt", "w") as output_file:
sum_of_numbers = sum(numbers)
if sum_of_numbers % 10 == 0:
output_file.write(str(sum_of_numbers))
else:
output_file.write("Bunday son yo'q")
except:
with open("output.txt", "w") as output_file:
output_file.write("Fayl yozishda xatolik yuz berdi")


39) “input.txt”faylida x1, y1 x2,y2 x3,y3 sonlar berilgan shu

Download 134.94 Kb.

Do'stlaringiz bilan baham:
1   2   3   4   5   6   7   8   9   10




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling