RockPaperScissors (4/25)
Where is 3/25???:
So I did start on project 3, which is a player vs player, version of "Guess the Number"(2/25), But I lost a little motivation on that project so either I will come back to it later, or leave it as it is now.
Endless Loops :)
So this week is all about Rock, paper and scissors, the game we all love.
So the way I chose to set up the game was by making two functions.
One for PC chosen:
#function for pcPickkk
def compPickFunction():
choice = random.randint(1,3)
if choice == 1:
choice = 'rock'
elif choice == 2:
choice = 'paper'
elif choice == 3:
choice = 'scissor'
return(choice)
As you can see, I decided to make a random.randint()
call and by the result I decided what it should end up with. Another approach I could have gone with could be the random number, choosing an item based on the length of a list. But it worked fine and did not have that big of trouble with it.
Player chose:
#Function for plPick
def PlayerPickFunction():
while True:
playerChoice = input(f'Please choose:{choises}')
playerChoice = playerChoice.lower()
try:
if playerChoice in choises:
break
except:
print(f'Oh please pick one of the possible choicse {choises}')
continue
return (playerChoice)
As you can see, I decided to make all input lowercase immediately after the input, and then I did the common try/except. Here I had the chance to try out the operation: "in" Where it checks if if playerChoice in choises:
this worked pretty nicely and made the job done. What you cannot see is the "global" variable: choises
. This is simply a list var with: Rock Scissor and paper.
the Game loop:
#Game Variable
gameRound = 0
computerPoint = 0
playerPoint = 0
#Game loop
while gameRound < 5:
print(f'We are at gameRound: {gameRound}')
pcPick = compPickFunction()
print(f'Pc have chosen {pcPick}')
plPick = PlayerPickFunction()
print(f'Player have chosen: {plPick}')
#if pcPick Rock
if pcPick == 'rock':
print('pcPick is a Rock')
if plPick == 'rock':
print('Now i start a new round')
continue
elif plPick == 'paper':
playerPoint += 1
gameRound += 1
continue
elif plPick == 'scissor':
computerPoint += 1
gameRound += 1
continue
Here is the main part of the game loop:
it starts with a check and print, of what gameRound
we are at. Then it first asks for pc choice, then player choice. and then the If...else starts up.
as you can see after each statement it decides if the player or computer should have a point, and raise the gameRound
with +1.
In actual pretty happy about how it ended, it was a fun fight, and deffently learned a lot about it. I got a few good endless loops into my terminal and a lot of frustration about indent. Needs to get used to it.
I'm also happy about my comment since I had a better view of the game.
That's it for Rock paper and scissors. Deffently a good practice game that I will recommend to everyone learning a new programming language.
Enjoy
Mattias