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

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

المطلوب

أكتب برنامج يطلب من المستخدم إدخال رقمين, ثم يعرض له قائمة خيارات يمكن تطبيقها على هذين الرقمين.
المستخدم سيكون عليه إدخال رقم الخيار فقط حتى يتم تنفيذه.

  • الخيار 0 لإيقاف البرنامج.
  • الخيار 1 لطباعة ناتج جمع العددين.
  • الخيار 2 لطباعة ناتج طرح العددين.
  • الخيار 3 لطباعة ناتج ضرب العددين.
  • الخيار 4 لطباعة ناتج قسمة العددين.

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

a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("\nWrite down the operation number then press enter\n"
"0: To end the program.\n"
"1: To print the sum.\n"
"2: To print the subtraction.\n"
"3: To print the multiplication.\n"
"4: To print the division.")
option = -1
while option != 0:
print("-------------------------")
option = int(input('Enter option: '))
if option == 0:
print("Program end.")
elif option == 1:
print(a, "+", b, " = ", a + b)
elif option == 2:
print(a, "-", b, " = ", a - b)
elif option == 3:
print(a, "*", b, " = ", a * b)
elif option == 4:
print(a, "/", b, " = ", a / b)
a = float(input("Enter first number: ")) b = float(input("Enter second number: ")) print("\nWrite down the operation number then press enter\n" "0: To end the program.\n" "1: To print the sum.\n" "2: To print the subtraction.\n" "3: To print the multiplication.\n" "4: To print the division.") option = -1 while option != 0: print("-------------------------") option = int(input('Enter option: ')) if option == 0: print("Program end.") elif option == 1: print(a, "+", b, " = ", a + b) elif option == 2: print(a, "-", b, " = ", a - b) elif option == 3: print(a, "*", b, " = ", a * b) elif option == 4: print(a, "/", b, " = ", a / b)

إذا قام المستخدم بإدخال الرقمين 1 و 4. ثم قام بتجربة كل الخيارات الموجودة في القائمة, فستكون النتيجة كالتالي.
قمنا بتعليم المعلومات التي أدخلها المستخدم باللون الأصفر و النتائج التي ظهرت له بعد إدخالها باللون الأزرق.

Enter first number:  1 
Enter second number:  4 

Write down the operation number then press enter
0: To end the program.
1: To print the sum.
2: To print the subtraction.
3: To print the multiplication.
4: To print the division.
-------------------------
Enter option:  1 
1 + 4 = 5
-------------------------
Enter option:  2 
1 - 4 = -3
-------------------------
Enter option:  3 
1 * 4 = 4
-------------------------
Enter option:  4 
1 / 4 = 0.25
-------------------------
Enter option:  0 
Program end.