I am a beginner in python. Started a week ago. So I created a basic Stone, paper and scissor game with user input and random guess for computer. See the program below.
import random
random_element = random.randrange(1,4)
computer = random_element
print("Computer have choosen their, Now it's your turn.")
player = int(input("Plyer's Turn: Enter (1) to choose Stone, (2) for Paper, and (3) for Scissor:\n"))
if player == 1:
player = 'Stone'
if player == 2:
player = 'Paper'
if player == 3:
player = 'Scissor'
if computer == 1:
computer = 'Stone'
if computer == 2:
computer = 'Paper'
if computer == 3:
computer = 'Scissor'
def game(computer,player):
bool = 1
if computer == player:
bool = None
elif computer == 1:
if player == 2:
bool = True
if player == 3:
bool = False
elif computer == 2:
if player == 3:
bool = True
if player == 1:
bool = False
elif computer == 3:
if player == 1:
bool = True
if player == 2:
bool = False
return bool
if player == 1:
player = 'Stone'
if player == 2:
player = 'Paper'
if player == 3:
player = 'Scissor'
answer = game(computer,player)
if answer == True:
print(f"Computer chosen: {computer} \nand You chosen: {player} \nSo, You Won the Game.")
if answer == False:
print(f"Computer chosen: {computer}\nand You chosen: {player}\nSo, You loose the Game.")
if answer == None:
print(f"Computer was chosen: {computer}\nand you choose: {player}\nIt is a Tie..")
But after some time to try this game. I realise that I won every time exept Tie. I don't know why. I check my program too many time and checks winning and loosing statement too many times. But I won't found my error.
Please help me. This is my first game.
If someone have good experience in python then answer me proper, simple but short methode (without importing too many modules).
CodePudding user response:
You have first assigned numbers to the variables computer
and player
. After that you change it to a string. but you looking for a number in the def game(computer,player)
function.
I didn't change your variable name or code structure. read all comments under your question and change the code as you wish.
import random
random_element = random.randrange(1,4)
computer = random_element
print("Computer have choosen their, Now it's your turn.")
player = int(input("Plyer's Turn: Enter (1) to choose Stone, (2) for Paper, and (3) for Scissor:\n"))
if player == 1:
player = 'Stone'
if player == 2:
player = 'Paper'
if player == 3:
player = 'Scissor'
if computer == 1:
computer = 'Stone'
if computer == 2:
computer = 'Paper'
if computer == 3:
computer = 'Scissor'
def game(computer,player):
bool = 1
if computer == player:
bool = None
elif computer == 'Stone':
if player == 'Paper':
bool = True
if player == 'Scissor':
bool = False
elif computer == 'Paper':
if player == 'Scissor':
bool = True
if player == 'Stone':
bool = False
elif computer == 'Scissor':
if player == 'Stone':
bool = True
if player == 'Paper':
bool = False
return bool
if player == 1:
player = 'Stone'
if player == 2:
player = 'Paper'
if player == 3:
player = 'Scissor'
print(computer)
answer = game(computer,player)
if answer == True:
print(f"Computer chosen: {computer} \nand You chosen: {player} \nSo, You Won the Game.")
if answer == False:
print(f"Computer chosen: {computer}\nand You chosen: {player}\nSo, You loose the Game.")
if answer == None:
print(f"Computer was chosen: {computer}\nand you choose: {player}\nIt is a Tie..")