ظهور رسالة خطاء

Ibrahim • منذ 5 سنوات

السلام عليكم ورحمة الله وبركاته

 

أخواني/تي يوجد هذا البرنامج عند التعديل عليه حاولت يستقبل حروف بدل ارقام تظهر لي رساله خطاء ممكن مساعده واكون لكم من الشاكرين

 

import random

def player_choice(user_choice): # Register and shows the players choice

if user_choice == 'r':

print("The player chose rock")

elif user_choice == 'p':

print("The player chose paper")

else:

print("The player chose scissors")

 

def computer_choice(cpu_choice): # Register and shows the cpus choice

if cpu_choice == 'r':

print("The computer chose rock")

elif cpu_choice == 'p':

print("The computer chose paper")

else:

print("The computer chose scissors")

 

def result(user_choice, cpu_choice, player_score, cpu_score): # Define the game result

if user_choice == cpu_choice:

return [player_score + 0.5, cpu_score + 0.5]

elif user_choice == 'r' and cpu_choice == 's':

return [player_score + 1, cpu_score]

elif user_choice == 'p' and cpu_choice == 'r':

return [player_score + 1, cpu_score]

elif user_choice == 's' and cpu_choice == 'p':

return [player_score + 1, cpu_score]

else:

return [player_score, cpu_score + 1]

 

def print_score(p_score, c_score): # Identifies the result and print the total score

print("Score:""\nPlayer:", p_score, "\nComputer:", c_score)

 

def validation_input(): # Validates the input

while True:

try:

user_input = str(input("Put your choice:\n"))

if user_input not in ['r', 'p', 's']:

print("We only accept commands between 1 and 3, according to the table, type again")

continue

if type(user_input) == str:

break

except ValueError:

print("We only accept exact numbers")

continue

return user_input

 

print('''1 - Rock

2 - Paper

3 - Scissors''') # Printing the instructions

human_score = 0

computer_score = 0

while True: # The condition is not important since the loop will stop on line 68 if the user wishes so

user = validation_input()

player_choice(user)

ai = random.choice ['r', 'p', 's']

computer_choice (ai)

human_score, computer_score = result(user, ai, human_score, computer_score) # Accumulate the score

print_score(human_score, computer_score)

command = str(input("Type 0 to stop the program, type any another number to keep playing"))

if command == 0:

break

كلمات دليلية: rps

ساعد بالإجابة

"إن في قضاء حوائج الناس لذة لا يَعرفها إلا من جربها، فافعل الخير مهما استصغرته فإنك لا تدري أي حسنة تدخلك الجنة."

الإجابات (1)

حمد آل دهش • منذ 5 سنوات

 كانت المشكلة في استدعاء random.choice بدون أقواس البرنامج بالكود المرفق شغال

import random

def player_choice(user_choice): # Register and shows the players choice

    if user_choice == 'r':
        print("The player chose rock")
    elif user_choice == 'p':
        print("The player chose paper")
    else:
        print("The player chose scissors")

 

def computer_choice(cpu_choice): # Register and shows the cpus choice

    if cpu_choice == 'r':
        print("The computer chose rock")
    elif cpu_choice == 'p':
        print("The computer chose paper")
    else:
        print("The computer chose scissors")

 
def result(user_choice, cpu_choice, player_score, cpu_score): # Define the game result

    if user_choice == cpu_choice:
        return [player_score + 0.5, cpu_score + 0.5]

    elif user_choice == 'r' and cpu_choice == 's':
        return [player_score + 1, cpu_score]

    elif user_choice == 'p' and cpu_choice == 'r':
        return [player_score + 1, cpu_score]

    elif user_choice == 's' and cpu_choice == 'p':
        return [player_score + 1, cpu_score]

    else:
        return [player_score, cpu_score + 1]

 

def print_score(p_score, c_score): # Identifies the result and print the total score
    print("Score:""\nPlayer:", p_score, "\nComputer:", c_score)


def validation_input(): # Validates the input

    while True:
        try:
            user_input = str(input("Put your choice:\n"))
            if user_input not in ['r', 'p', 's']:
                print("We only accept commands between 1 and 3, according to the table, type again")
                continue
            if type(user_input) == str:
                break
        except ValueError:
            print("We only accept exact numbers")
        continue
    return user_input

 
print('''1 - Rock

2 - Paper

3 - Scissors''') # Printing the instructions

human_score = 0

computer_score = 0

while True: # The condition is not important since the loop will stop on line 68 if the user wishes so
    user = validation_input()
    player_choice(user)
    ai = random.choice (['r', 'p', 's'])

    computer_choice (ai)
    human_score, computer_score = result(user, ai, human_score, computer_score) # Accumulate the score
    print_score(human_score, computer_score)
    command = str(input("Type 0 to stop the program, type any another number to keep playing"))

    if command == "0":
        break

 

لايوجد لديك حساب في عالم البرمجة؟

تحب تنضم لعالم البرمجة؟ وتنشئ عالمك الخاص، تنشر المقالات، الدورات، تشارك المبرمجين وتساعد الآخرين، اشترك الآن بخطوات يسيرة !