2.4 אבן, נייר, ומספריים פתרון
import random
print("Welcome to Rock, Paper, Scissors game!\nTo quit the game enter 'quit' or 'q'.")
computer_wins = 0
human_wins = 0
while True:
print(f"\nScores: Computer - {computer_wins} wins, Human - {human_wins} wins.\n")
user_input = input("Enter (Rock, Paper, or Scissors): ").lower()
if user_input == "quit" or user_input == "q":
final_message = ""
if computer_wins > human_wins:
final_message = "Human won!"
elif human_wins < computer_wins:
final_message = "Computer won!"
else:
final_message = "Draw!"
print(f"\nFinal score: {final_message}")
break
choices = ["rock", "paper", "scissors"]
computer_choice = random.choice(choices)
if user_input.lower() not in choices:
print("Invalid choice. Please enter Rock, Paper, or Scissors.")
continue
human_choice = user_input.lower()
print(f"\nHuman: {human_choice}")
print(f"Computer: {computer_choice}")
if computer_choice == human_choice:
print("It's a draw!")
elif (
(computer_choice == "rock" and human_choice == "scissors") or
(computer_choice == "scissors" and human_choice == "paper") or
(computer_choice == "paper" and human_choice == "rock")
):
print("Computer won!")
computer_wins += 1
else:
print("Human won!")
human_wins += 1
- תפתח את הקוד בPyCharm בשביל קריאות יותר טובה.
- הקוד הזה מאוד מסובך נכון? בפרק הבא נלמד המון דרכים לפשט את הקודים שלנו ולכתוב אותם בצורה יותר קלה וברורה.