تمارين تجميع النصوص برمجياً
عدّل هذا البرنامج:
name = 'World' line = '' for char in name: line = line + char print(line) بحيث يضيف مسافة بعد كل محرف في المثلث، فيصبح الناتج هكذا:
W W o W o r W o r l W o r l d
You will need to use one more
+.You will need to use a string consisting of one space:
' '.
رائع! عدّل البرنامج الآن بحيث تكون كل سطر مكتوبًا بالعكس، هكذا:
W oW roW lroW dlroW
The solution is very similar to the original triangle program, just make one small change.
You still want to add one character to
lineat a time, it's just a question of where you add it.You want the lines to be reversed, so you need to reverse/flip something.
You need to add the character before the string, instead of after.
3 + 7 is equal to 7 + 3. Same for all numbers. Is this also true for strings?
ممتاز!
الكود من الشكل:
line = line + char شائع جدًا، لذلك يسمح بايثون بكتابته بصورة مختصرة. السطر التالي يعني الشيء نفسه:
line += char
لاحظ أنه لا توجد صيغة مختصرة مماثلة للسطر line = char + line.
استخدم الآن += مع حلقة for لكتابة برنامج يطبع name وتحته خط، هكذا:
World
يجب أن توجد شرطة - واحدة مقابل كل محرف في name.
Look at the triangle program for inspiration.
Look at the program where you printed
nameonce for each character for inspiration.You will need to build up a string of dashes (
-) one character at a time.The for loop will create a variable such as
char, but the program doesn't need to use it.
ممتاز جدًا!
وبالمناسبة، عندما لا تحتاج إلى استخدام متغير معين، فمن المعتاد تسمية ذلك المتغير _، مثل for _ in name:. هذا لا يغير طريقة تنفيذ البرنامج، لكنه يوضح للقارئ أنك لا تحتاج إلى قيمة المتغير.
لنجعل البرنامج أكثر أناقة. وسّعه بحيث يرسم صندوقًا حول الاسم، هكذا:
+-----+ |World| +-----+
You did all the hard stuff in the previous exercise. Now it's just some simple adding of strings.
You only need one for loop - the one used to make the line of dashes from the previous exercise.
Don't try and do everything at once. Break the problem up into smaller, easier subproblems.
Since you need to output three separate lines of text, you will need to call
print()three times.
أصبحت جيدًا في هذا! حان وقت تحدٍّ أصعب قليلًا. بدل أن نضع الاسم داخل صندوق، اجعل الاسم نفسه جزءًا من الصندوق. اكتب برنامجًا ينتج:
+World+ W W o o r r l l d d +World+
You will need two separate for loops over
name.Each line except for the first and last has the same characters in the middle. That means you can reuse something.
Create a variable containing the spaces in the middle and use it many times.
Use one loop to create a bunch of spaces, and a second loop to print a bunch of lines using the previously created spaces.
أحسنت! أصبحت الفكرة واضحة لديك بالفعل.
إذا أردت، يمكنك حل تحدٍّ إضافي اختياري أدناه. وإذا لم ترغب الآن، يمكنك الانتقال إلى الدرس التالي والعودة إلى هذا التحدي لاحقًا.
حاول كتابة برنامج يطبع name في خط قطري، مثل:
W o r l d
The first letter should have 0 spaces before it, the second letter should have 1 space before it, the third should have 2, etc.
You should keep the spaces in a variable and build them up in a loop, as before.
The difference is that you need to print letters at the same time as building up spaces.
In other words, you need a single loop that does both.
The body of the loop needs to print the spaces and letter, and also add a space.
Since the first letter should have no spaces before it, you need to add a space after printing a letter.
ممتاز جدًا! استمر بهذا المستوى.