اذهب إلى المحتوى

السؤال

Recommended Posts

  • 0
نشر

لاحظ أن البرنامج لا يغلق من تلقاء نفسه ويقوم بإكمال الكود دائما ما لم يحدث خطأ أو يتم إيقافه من خلال دالة exit .

لاحظ أنه ستواجهك مشكلة في سطر  19 لأنك تحاول إستخدام المتغير computer_result ولكن هذا المتغير تم تعريفه في جزء جملة if أى أنه إذا لم يتحقق الشرط وتم تنفيذ else فهنا ستحدث مشكلة لأن المتغير لم يتم تعريفه.

ولهذا يجب إغلاق الكود في جزء else من خلال exit() هكذا :

import random

choose = input(
    "welcome to the coin guessing game!\nchoose a method to toss the coin\n1.using random.random()\n2.using random.randint()\nenter your choice (1 or 2):\n"
)
if choose == "1":
    if random.random() >= 0.5:
        computer_result = "heads"
    else:
        computer_result = "tails"
elif choose == "2":
    if random.randint(0, 1) == 0:
        computer_result = "heads"
    else:
        computer_result = "tails"
else:
    print("invalid choice. Please select either 1 or 2")
    exit()
user_choice = input("enter your guess (heads or tails):\n")
if user_choice.lower() == computer_result.lower():
    print("congratulations! you won")
    print(f"the computer's coin toss result was: {computer_result}")
else:
    print("sorry, you lost")
    print(f"the computer's coin toss result was: {computer_result}")

 

انضم إلى النقاش

يمكنك أن تنشر الآن وتسجل لاحقًا. إذا كان لديك حساب، فسجل الدخول الآن لتنشر باسم حسابك.

زائر
أجب على هذا السؤال...

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   جرى استعادة المحتوى السابق..   امسح المحرر

×   You cannot paste images directly. Upload or insert images from URL.

  • إعلانات

  • تابعنا على



×
×
  • أضف...