Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الأول - حل التمرين الثالث بلغة بايثون

المطلوب

أكتب برنامج مهمته رسم الشكل التالي بواسطة الحلقات.
عند تشغيل البرنامج, يجب أن يطلب من المستخدم إدخال عدد أسطر الشكل الذي سيتم رسمه.
إنتبه: يجب أن يدخل المستخدم عدد أكبر من صفر, لأن عدد الأسطر لا يمكن أن يكون صفر أو أقل من صفر.


الحل بلغة بايثون

الطريقة الأولى لحل التمرين.

n = 0
while n <= 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n+1):
print('*' * (n-i+1))
n = 0 while n <= 0: n = int(input("Enter the number of lines: ")) for i in range(1, n+1): print('*' * (n-i+1))

الطريقة الثانية لحل التمرين و الحصول على نفس النتيجة.

n = 0
while n <= 0:
n = int(input("Enter the number of lines: "))
for i in range(1, n+1):
for j in range(1, n+2-i):
print('*', end='')
print()
n = 0 while n <= 0: n = int(input("Enter the number of lines: ")) for i in range(1, n+1): for j in range(1, n+2-i): print('*', end='') print()

الطريقة الثالثة لحل التمرين و الحصول على نفس النتيجة.

n = 0
while n <= 0:
n = int(input("Enter the number of lines: "))
i = 1
while i <= n:
j = 1
while j <= n+1-i:
print('*', end='')
j += 1
print()
i += 1
n = 0 while n <= 0: n = int(input("Enter the number of lines: ")) i = 1 while i <= n: j = 1 while j <= n+1-i: print('*', end='') j += 1 print() i += 1

سنحصل على النتيجة التالية إذا قام المستخدم بإدخال الرقم 5 عند التشغيل.

Enter the number of lines: 5
*****
****
***
**
*