sonlardan foydalanib, graph modulidan foydalangan holda
uchburchak chizib bersin. Uchburchak chizib bo’lmasa
“
output.txt” fayliga “Uchburchak yasab bo’lmaydi” .
Dasturni yozishda try... exceptdan foydalaning
import
random
import matplotlib.pyplot
as plt
try:
with open("input.txt", "w") as input_file:
points =[]
for _ in range(3):
x = random.randint(0, 10)
y = random.randint(0, 10)
point = f"{x},{y}"
input_file.write(point + "\n")
points.append((x, y))
x_values = [point[0] for point in points]
y_values = [point[1] for point in points]
plt.plot(x_values, y_values, 'ro-')
plt.xlabel('X')
plt.ylabel('Y')
with open("output.txt", "w") as output_file:
plt.savefig("triangle.png")
output_file.write("Uchburchak chizib bo'ldi")
except:
with open("output.txt", "w") as output_file:
output_file.write("Uchburchak yasab bo'lmaydi")
40) “input.txt” fayliga 50 ta [10,70] orasidagi sonni yozsin
va yozilgan sonlardan satriy ketma-ketlik hosil qilib, uning
raqamlari yig’indisini “output.txt” fayliga yozilsin
import random
try:
with open("input.txt", "w") as input_file:
numbers = []
for _ in range(50):
number = random.randint(10, 70)
input_file.write(str(number) + "\n")
numbers.append(number)
concatenated_string = "".join(str(number) for number in numbers)
sum_of_digits = sum(int(digit) for digit in concatenated_string)
with open("output.txt", "w") as output_file:
output_file.write(str(sum_of_digits))
except:
with open("output.txt", "w") as output_file:
output_file.write("Fayl yozishda xatolik yuz berdi")