المطلوب
أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أسطر الشكل الذي سيتم رسمه.
إنتبه: يجب أن يدخل المستخدم عدد مفرد و أكبر من صفر, لأن عدد الأسطر لا يمكن أن يكون مزدوجاً أو قيمته تساوي صفر أو أقل من صفر.
الحل بلغة بايثون
الطريقة الأولى لحل التمرين.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n + 1):
if i <= n / 2:
print(' ' * (int(n / 2) - i + 1), end='')
print('*' * i, end='')
else:
print(' ' * (i - int(n / 2) - 1), end='')
print('*' * (n - i + 1), end='')
print()
الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n+1):
if i <= n / 2:
for k in range(1, int(n / 2) - i + 2):
print(' ', end='')
for j in range(1, i + 1):
print('*', end='')
else:
for k in range(1, i - int(n / 2)):
print(' ', end='')
for j in range(1, n - i + 2):
print('*', end='')
print()
الطريقة الثالثة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
i = 1
while i <= n:
if i <= n / 2:
k = 1
while k < int(n / 2) - i + 2:
print(' ', end='')
k += 1
j = 1
while j <= i:
print('*', end='')
j += 1
else:
k = 1
while k < i - int(n / 2):
print(' ', end='')
k += 1
j = 1
while j <= n - i + 1:
print('*', end='')
j += 1
print()
i += 1
الطريقة الرابعة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, int(n / 2) + 2):
print(' ' * (int(n / 2) - i + 1), end='')
print('*' * i, end='')
print()
for i in range(1, int(n / 2) + 1):
print(' ' * i, end='')
print('*' * (int(n/2) + 1 - i), end='')
print()
الطريقة الخامسة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
for i in range(1, int(n / 2) + 2):
for k in range(1, int(n / 2) - i + 2):
print(' ', end='')
for j in range(1, i + 1):
print('*', end='')
print()
for i in range(1, int(n / 2) + 1):
for k in range(1, i + 1):
print(' ', end='')
for j in range(1, int(n / 2) + 2 - i):
print('*', end='')
print()
الطريقة السادسة لحل التمرين و الحصول على نفس النتيجة.
n = 0
while n <= 0 or n % 2 == 0:
n = int(input("Enter the number of lines: "))
i = 1
while i <= int(n / 2) + 1:
k = 1
while k <= int(n / 2) - i + 1:
print(' ', end='')
k += 1
j = 1
while j <= i:
print('*', end='')
j += 1
print()
i += 1
i = 1
while i <= int(n / 2):
k = 1
while k <= i:
print(' ', end='')
k += 1
j = 1
while j <= int(n / 2) + 1 - i:
print('*', end='')
j +=1
print()
i += 1
سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 عند التشغيل.
Enter the number of lines: 5 * ** *** ** *



محرر الويب
نظام الألوان
محول الوحدات
محلل عناوين الشبكات