Here is a simple code for a text-based rock-paper-scissors game in Python:
print("Welcome to Rock-Paper-Scissors!")
while True:
print("Enter choice \n1 for Rock \n2 for paper \n3 for scissor")
choice = int(input("User turn: "))
while choice > 3 or choice < 1:
choice = int(input("Enter valid input: "))
if choice == 1:
choice_name = 'rock'
elif choice == 2:
choice_name = 'paper'
else:
choice_name = 'scissor'
print("user chose: " + choice_name)
print("\nNow its computer turn.......")
import random
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice_name = 'rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
else:
comp_choice_name = 'scissor'
print("Computer chose: " + comp_choice_name)
print(choice_name + " V/s " + comp_choice_name)
result = (choice - comp_choice + 3) % 3
if result == 0:
print("Tie")
elif result == 1:
print("You lose!", comp_choice_name, "covers", choice_name)
else:
print("You win!", choice_name, "smashes", comp_choice_name)
ans = input("\nDo you want to play again? (Y/N)")
if ans == 'n' or ans == 'N':
break
print("\nThanks for playing!")
This code uses a while loop to repeat the game until the user decides to stop. The game allows the user to enter their choice and generates a random choice for the computer. The result is then calculated based on the comparison between the user's and computer's choices, and the winner is announced.
print("Welcome to Rock-Paper-Scissors!")
while True:
print("Enter choice \n1 for Rock \n2 for paper \n3 for scissor")
choice = int(input("User turn: "))
while choice > 3 or choice < 1:
choice = int(input("Enter valid input: "))
if choice == 1:
choice_name = 'rock'
elif choice == 2:
choice_name = 'paper'
else:
choice_name = 'scissor'
print("user chose: " + choice_name)
print("\nNow its computer turn.......")
import random
comp_choice = random.randint(1, 3)
if comp_choice == 1:
comp_choice_name = 'rock'
elif comp_choice == 2:
comp_choice_name = 'paper'
else:
comp_choice_name = 'scissor'
print("Computer chose: " + comp_choice_name)
print(choice_name + " V/s " + comp_choice_name)
result = (choice - comp_choice + 3) % 3
if result == 0:
print("Tie")
elif result == 1:
print("You lose!", comp_choice_name, "covers", choice_name)
else:
print("You win!", choice_name, "smashes", comp_choice_name)
ans = input("\nDo you want to play again? (Y/N)")
if ans == 'n' or ans == 'N':
break
print("\nThanks for playing!")
This code uses a while loop to repeat the game until the user decides to stop. The game allows the user to enter their choice and generates a random choice for the computer. The result is then calculated based on the comparison between the user's and computer's choices, and the winner is announced.