2019-06-13
  • |
  • daafoor
  • |
  • مشاهدات: 46287

 امثلة على استخدام if-else في python

 

برنامج يطبع العدد الاكبر بين عددين:

a = 33
b = 200
if b > a:
  print("b is greater than a")

 

نفس المثال لكن مكتوب بكود ادق اكثر.

a = 200
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

 

برنامج بايثون يطلب من الطالب ادخال علامته ومن ثم يطبع النتيجة اما ناجح او راسب:

#write python program that prompts student to enter his mark, then prints the result, either "pass" or "fail"
mark=int(input("enter your mark:"))
if mark>=60:
    print("pass")
else:
    print("fail")

 

نفس البرنامج السابق مع اضافة بعض التحسينات, العلامة يجب ان تكون بين 0 الى 100 حصرا, والا يطبع رسالة خطأ : 

#write the same previous program but with enhancements: mark should be between 0 and 100, otherwise print "invalid entry"
mark=int(input("enter your mark:"))
if mark<=100 and mark>=60:
    print("pass")
else:
    print("fail")

 

ايضا ممكن نكتبها بشكل اخر:

#in another way:
mark=int(input("enter your mark:"))
if mark<=100 and mark>=60:
    print("pass")
elif mark>=0 and mark

 

وشكل اخر ايضا:

#also in another way:
mark=int(input("enter your mark "))
if mark>=60:
    print("pass")
elif mark

 

برنامج يطلب من المستخدم ادخال رقم ومن ثم يتحقق ما اذا كان الرقم موجب او سالب:

#write python program to check if the number entered by is positive or negative
number=int(input("enter number: "))
if number>=0:
    print("number is positive")
else:
    print("number is negative")

 

برنامج يطلب من المستخدم ادخال رقم ومن ثم يتحقق ما اذا كان الرقم المدخل زوجي او فردي:

#write python program to check if the number entered by user is even or odd
number=int(input("enter number: "))
if number%2==0:
    print("number is even")
else:
    print("number is odd")

 

 

 

Test if a is greater than b, AND if c is greater than a:

a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True")

 

Test if a is greater than b, OR if a is greater than c:

#Test if a is greater than b, OR if a is greater than c:
a = 10
b = 33
c = 500
if a > b or a > c:
  print("At least one of the conditions is True")
else:
    print("else is executed")
print("bye")

 

برنامج يقارن بين عددين ويطبع من هو الاكبر.

a = 33
b = 33
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")

 

 

اكتب برنامج بلغة بايثون يقرأ عدد من المستخدم ومن ثم يقرر ما اذا كان العدد المدخل هو عدد زوجي ام عدد فردي.

ملاحظة: العدد الزوجي هو اي عدد يقبل القسمة على 2, 

#write python program to read a number from keyboard then check if it is odd or even
num=int(input('please enter number'))
if num%2==0:
    print('number is even')
else: print('number is odd')

 

 اكتب برنامج بلغة بايثون ليقرأ عدد من المستخدم ومن ثم يتأكد من انه موجب ام سالب.

#write python program to read number from user then check if it is positive or negative
num=int(input('please enter number'))
if num>=0:
    print('number is positive')
else: print('number is negative')

 

اكتب برنامج بايثون يقرأ من الكيبورد علامة الطالب, ومن ثم يطبع نتيجة الطالب ان كان ناجح ام راسب.

#write python program to read student mark and check if he passed or failed(pass mark is 60 or higher)
num=int(input('please enter student mark'))
if num>=60:
    print('student passed')
else:
 print('student failed')

 

لعبة تحزير الارقام guess number

يقوم المستخدم بادخال رقم عشوائي, ليتحقق البرنامج بدوره من ان هذا الرقم يتطابق مع الرقم المخزن بذاكرة البرنامج ام لا.

#guess game
correctNumber=int(input("please enter the number "))
guess=int(input("enter your guess:"))
if guess==correctNumber:
    print("congratulations,, you guessed it!")
elif guess>correctNumber:
    print("wrong number, your number is bigger than correct number")
elif guess

 

 اكتب برنامج باستخدام بايثون ليقرأ رقم اليوم بالاسبوع ومن ثم يطبع اسم اليوم

#write a python program to read the number of week's day then prints the name of equavalent day name
num_of_day=int(input('enter the number of day, between 1 to 7'))

if num_of_day == 1:
  print('saturday')
elif num_of_day == 2:
  print('sunday')
elif num_of_day == 3:
  print('monday')
elif num_of_day == 4:
  print('tuesday')
elif num_of_day == 5:
  print('wednsday')
elif num_of_day == 6:
  print('thursday')
elif num_of_day == 7:
  print('friday')
else:
  print('invalid entry')

 

guess game example

#write guess game using python

name  = 'meshal'
guess = input('Enter your guess: ')
if guess == name:
  # New block starts here
  print('Congratulations, you guessed it.')
  print('(but you do not win any prizes!)')
  # New block ends here
else: 
 print('wrong guess.')
print('Done')

 

 

 

اكتب برنامج بايثون يطلب من الطالب ادخال علامته الرقمية من 100, ومن ثم يقوم بحساب وطباعة درجة الطالب الموافقة للعلامة.

#Exercise : Write python program that prints the letter grade (A+, A, B+, B, C+, C, D+, D, or F)
# relative to a grade g given by the user.

mark=int(input('enter the mark'))
if mark>=95:
    print('the grde is A+')
elif mark>=90:
    print('A')
elif mark>=85:
    print('B+')
elif mark >= 80:
    print('B')
elif mark >= 75:
    print('C+')
elif mark >= 70:
     print('C')
elif mark >=65:
     print('D+')
elif mark >= 60:
     print('D')
else:
    print('F')

 

اكتب برنامج بايثون يتحقق من ان اسم اليوم المدخل من المستخدم هو يوم عطلة ام يوم

#Exercise : Write python program that determines whether a day given by the user is a workday or weekend day.

import sys

day=input('enter the day')
if day=='saturday' || day == 'friday':
    print('weekend')
elif day == 'sunday' || day == 'monday' || day == 'tuesday' || day == 'wednsday' || day == 'thuresday':
    print('workday')
else:
    print('invalid entry')
    sys.exit()

 

 

الفيديو التالي يشرح الامثلة السابقة بالتفصيل:

 

ابحث عن مسائل برمجة بايثون | Python programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول