تحديات برمجيةالتحدي الثالث - حل التمرين الرابع بلغة بايثون
المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال نصّين, بعدها يقوم بطباعة ما إن كان النص الأول يبدأ بالنص الثاني أم لا.
مثال
Enter text 1: I like programming. Enter text 2: Hello -------------------------------- Text 1 is not start with 'Hello'
الحل بلغة بايثون
text1 = input("Enter text 1: ")
text2 = input("Enter text 2: ")
found = True
i = 0
while i < len(text2) and found:
if text1[i] != text2[i]:
found = False
i += 1
print('--------------------------------')
if found:
print("Text 1 start with '" + text2 + "'")
else:
print("Text 1 is not start with '" + text2 + "'")
text1 = input("Enter text 1: ")
text2 = input("Enter text 2: ")
found = True
i = 0
while i < len(text2) and found:
if text1[i] != text2[i]:
found = False
i += 1
print('--------------------------------')
if found:
print("Text 1 start with '" + text2 + "'")
else:
print("Text 1 is not start with '" + text2 + "'")
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'