Learn practical skills, build real-world projects, and advance your career
Created 4 years ago
#Guess the Number
import random
#step1 : generate a random number : computerSimulation
## step2: ask the user for the input
computerChoice= random.randint(1,20) # start,end
for _ in range(6):
playerChoice= int(input('guess the number\n'))
if playerChoice==computerChoice:
print('gotcha! you made it!')
break
elif playerChoice<computerChoice:
print("your guess is too low")
elif playerChoice>computerChoice:
print("your gues is to high")
else:
print("you have run out of attempts ! please try again")
guess the number
7
your gues is to high
guess the number
5
your gues is to high
guess the number
3
your gues is to high
guess the number
2
your gues is to high
guess the number
1
gotcha! you made it!
# Rock Paper Scissor
# 2 Players
# rock/paper/scissor
# player1 : ask the user to enter a character 'r'/'p'/'s'
# if the player enter a character other then 'r'/'p'/'s'
# ask the user to input again..
#computerChoice = generate a random number between(1-3)
computerChoice=''
if the number is 1
commputerChoice='r'
elif the number is 2
computerChoirce='s'
...
#now compare the playerchoice with computer choice
# if player1 choice is rock and computerChoice is scissor player 1 wins.
# if player1 choice is paper and computerchoice is rock player1 wins
# if player1 choice is scissor and computerchoice is paper player1 wins.
# if player1 choice matches with player2 choice then print its a tie.
import random, sys
print('ROCK, PAPER, SCISSORS')
# These variables keep track of the number of wins, losses, and ties.
wins = 0
losses = 0
ties = 0
while True: # The main game loop.
print('%s Wins, %s Losses, %s Ties' % (wins, losses, ties))
while True: # The player input loop.
print('Enter your move: (r)ock (p)aper (s)cissors or (q)uit')
playerMove = input()
if playerMove == 'q':
sys.exit() # Quit the program.
if playerMove == 'r' or playerMove == 'p' or playerMove == 's':
break # Break out of the player input loop.
print('Type one of r, p, s, or q.')
# Display what the player chose:
if playerMove == 'r':
print('ROCK versus...')
elif playerMove == 'p':
print('PAPER versus...')
elif playerMove == 's':
print('SCISSORS versus...')
# Display what the computer chose:
randomNumber = random.randint(1, 3)
if randomNumber == 1:
computerMove = 'r'
print('ROCK')
elif randomNumber == 2:
computerMove = 'p'
print('PAPER')
elif randomNumber == 3:
computerMove = 's'
print('SCISSORS')
# Display and record the win/loss/tie:
if playerMove == computerMove:
print('It is a tie!')
ties = ties + 1
elif playerMove == 'r' and computerMove == 's':
print('You win!')
wins = wins + 1
elif playerMove == 'p' and computerMove == 'r':
print('You win!')
wins = wins + 1
elif playerMove == 's' and computerMove == 'p':
print('You win!')
wins = wins + 1
elif playerMove == 'r' and computerMove == 'p':
print('You lose!')
losses = losses + 1
elif playerMove == 'p' and computerMove == 's':
print('You lose!')
losses = losses + 1
elif playerMove == 's' and computerMove == 'r':
print('You lose!')
losses = losses + 1