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

مشكلة في مقارنة النتيجة في لعبة التخمين على العملة في كود بايثون

Joseph Joseph

السؤال

import random

print("welcom to the coin Guessing Game")
print("choose a method to toss the coin:")
print("1. using random.random()")
print("2. using random.randint()")
choice = input("Enter your choie (1 or 2): ")
#random.randint
if choice=="1":
  random_1 = random.random()
  if random_1>=0.5:
    computer_result="tails"
  else:
    computer_result="heads"
elif choice=="2":
  if random.randint(0 , 1) == 0:
    computer_result="tails"
  else:
    computer_result="heads"
else:
  print("invalid choice :please select 1 or 2")

user_choice= input("Enter your guess (Heads or atails) ")
if user_choice.lower==computer_result.lower():
  print("congratulation . you won")
else:
  print("sorry you lost")


 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

Recommended Posts

  • 0

الكود قريب من الصحيح، ولكن هناك مشكلة صغيرة في الشرط الذي يقارن فيه اختيار المستخدم بنتيجة الكمبيوتر، ويجب عليك استخدام الدالة lower() لتحويل الحروف الصغيرة إلى الأحرف الصغيرة قبل مقارنتهم.

أي يجب أن يكون الشرط كالتالي:

if user_choice.lower() == computer_result.lower():

أيضًا، يجب أن تتحقق من أن القيم المدخلة من قبل المستخدم ("Heads" أو "Tails") تكون مكتوبة بشكل صحيح، لذلك عليك تعديل السطر الخاص بإدخال المستخدم كالتالي:

user_choice = input("Enter your guess (Heads or Tails): ").lower()

وبهذا الشكل، ستتمكن من تصحيح المشكلة في الشرط وضمان أنه سيتم مقارنة الإجابة بشكل صحيح بغض النظر عن الأحرف الكبيرة أو الصغيرة.

import random

print("Welcome to the Coin Guessing Game")
print("Choose a method to toss the coin:")
print("1. using random.random()")
print("2. using random.randint()")
choice = input("Enter your choice (1 or 2): ")

if choice == "1":
    random_1 = random.random()
    if random_1 >= 0.5:
        computer_result = "Tails"
    else:
        computer_result = "Heads"
elif choice == "2":
    if random.randint(0, 1) == 0:
        computer_result = "Tails"
    else:
        computer_result = "Heads"
else:
    print("Invalid choice: please select 1 or 2")
    exit()

user_choice = input("Enter your guess (Heads or Tails): ").capitalize()

if user_choice == computer_result:
    print("Congratulations, you won!")
else:
    print("Sorry, you lost. The coin landed on", computer_result)

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

  • 0

المشكلة في الكود تكمن في الطريقة التي تقوم بها بمقارنة اختيار المستخدم بنتيجة الكمبيوتر. يجب استخدام

user_choice.lower()

بدلاً من

user_choice.lower

للقيام بمقارنة السلاسل بشكل صحيح. يجب أن يتم استدعاء الدالة

lower()

لتحويل السلسلة إلى حروف صغيرة ومن ثم مقارنتها بالنتيجة العشوائية للكمبيوتر. 

import random

print("Welcome to the Coin Guessing Game")
print("Choose a method to toss the coin:")
print("1. using random.random()")
print("2. using random.randint()")
choice = input("Enter your choice (1 or 2): ")

# Random toss
if choice == "1":
    random_1 = random.random()
    if random_1 >= 0.5:
        computer_result = "tails"
    else:
        computer_result = "heads"
elif choice == "2":
    if random.randint(0, 1) == 0:
        computer_result = "tails"
    else:
        computer_result = "heads"
else:
    print("Invalid choice: please select 1 or 2")

user_choice = input("Enter your guess (Heads or Tails): ")

# Correct the comparison here, use lower() to make it case-insensitive
if user_choice.lower() == computer_result.lower():
    print("Congratulations, you won!")
else:
    print("Sorry, you lost.")

 

رابط هذا التعليق
شارك على الشبكات الإجتماعية

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

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

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

×   لقد أضفت محتوى بخط أو تنسيق مختلف.   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.

  • إعلانات

  • تابعنا على



×
×
  • أضف...