بايثون

قم بإنشاء لعبة اختبار باستخدام Python

قم بأنشاء لعبة مسابقة تقوم بطرح أسئلة على اللاعب عن الحيوانات. اللاعب لديه ثلاثة فرص فقط للإجابة على كل سؤال مما يعطي للعبة اكثر تحدي وصعوبة. كل إجابة صحيحة ستحرز نقطة الى رصيد اللاعب حتى في نهاية اللعبة سيكشف البرنامج النتيجة النهائية للاعب.

حان الوقت الآن لإنشاء الاختبار الخاص بك! أولاً ، سأقوم بإنشاء الأسئلة وآلية التحقق من الإجابة. بعد ذلك ، سأضيف الكود الذي يمنح اللاعب ثلاث محاولات للإجابة على كل سؤال:

الكود البرمجي

def check_guess(guess, answer):
    global score
    still_guessing = True
    attempt = 0
    while still_guessing and attempt < 3:
        if guess.lower() == answer.lower():
            print("Correct Answer")
            score = score + 1
            still_guessing = False
        else:
            if attempt < 2:
                guess = input("Sorry Wrong Answer, try again")
            attempt = attempt + 1
    if attempt == 3:
        print("The Correct answer is ",answer )
    
score = 0
print("Guess the Animal")
guess1 = input("Which bear lives at the North Pole? ")
check_guess(guess1, "polar bear")
guess2 = input("Which is the fastest land animal? ")
check_guess(guess2, "Cheetah")
guess3 = input("Which is the larget animal? ")
check_guess(guess3, "Blue Whale")
print("Your Score is "+ str(score))

مثال:

Correct Answer
Which is the fastest land animal? cheetah
Correct Answer
Which is the larget animal? blue whale
Correct Answer
Your Score is 3

مقالات ذات صلة

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *