RESOURCES:
I haven't posted in a bit, so I've added resources from the last 2 weeks:
Week 8 Slides: https://docs.google.com/presentation/d/1eul03VByEhgR7TDKSvk373H5dxwkN_gDroNlLmq_YOI/edit?usp=sharing
Week 9 Slides:
https://docs.google.com/presentation/d/1sjEbay9nKKXypizMsMhUlKzzG4M9fnVBbPuC5V5gM2k/edit?usp=sharing
Week 10 Slides:
https://docs.google.com/presentation/d/1tyDKdw8DQenLrrty3HjntSAm-okXjBDbWo6rWHFGVmc/edit?usp=sharing
Example code:
Week 8: https://replit.com/@ShravyaS/IntroToPython-8
Week 9: https://replit.com/@ShravyaS/IntroToPython-9
Week 10: https://replit.com/@ShravyaS/IntroToPython-10
MINI PROJECT:
For this project, please create a program that plays rock paper scissors with the user. The program should keep prompting the user for their move and keep playing games of rock paper scissors until the user enters "stop". Once "stop" is entered, print out the total number of wins and the total number of losses and the total number of draws.
- Use the random module, while loop, use input/print, and use if statements
Sample output: https://asciinema.org/a/AgNJ6i68mN3KkEvEvYU06Cc87
import random print("Let's play rock, paper, scissors!"); print('Please choose 0 for rock, 1 for paper, 2 for scissors, and type STOP to end'); user_input = input(); draw = 0; lose = 0; win = 0; while (user_input != "STOP"): user_input = int(user_input); my_choice = random.randrange(0,2,1); if (user_input == my_choice): print(f"my choice is also {my_choice}"); print("It's a draw"); draw = draw + 1; elif (user_input == 0): print(f"my choice is {my_choice}"); if (my_choice == 1): print("You lose"); lose = lose + 1; else: print("You win"); win = win + 1; elif (user_input == 1): print(f"my choice is {my_choice}"); if (my_choice == 2): print("You lose"); lose = lose + 1; else: print("You win"); win = win + 1; elif (user_input == 2): print(f"my choice is {my_choice}"); if (my_choice == 0): print("You lose"); lose = lose + 1; else: print("You win"); win = win + 1; else: print("invalid comparison"); print(f"my choice is {my_choice}"); print(f"user input is {user_input}"); print("\nContinue next round"); user_input = input(); total = win + draw + lose; print(f"You have played {total} games in total"); print(f"You win {win} times"); print(f"You lose {lose} times"); print(f"You draw {draw} times");
If you'd like a shorter solution, here's one I coded:
import random win = 0 loss = 0 draw = 0 userChoice = "" while True: compChoice = random.randint(1, 3) userChoice = input("Rock, paper, or scissors? ") if userChoice.lower() == "rock": userChoice = 1 elif userChoice.lower() == "paper": userChoice = 2 elif userChoice.lower() == "scissors": userChoice = 3 elif userChoice.lower() == "stop": print(f"You finished with {win} wins, {loss} losses, and {draw} draws.") break else: print("Enter a valid choice next time.") continue if userChoice == compChoice: draw += 1 print("Draw :\\") elif (userChoice % 3) > (compChoice % 3): win += 1 print("You win :D") else: loss += 1 print("You lose :(")
Solution:
I used a list in here for simplicity and I used a function, which basically you define, and then use in the program
import random moves = [1, 2, 3] moveN = ["Rock", "Paper", "Scissors"] wins = 0 wstreak = 0 hwstreak = 0 ties = 0 games = 0 def checkplay(human, robot): global ties, wins, hwstreak, wstreak if human == robot: print("Tie!") ties += 1 elif human == 1: if robot == 2: print("You Lose!") elif robot == 3: print("You Win!") wins += 1 elif human == 2: if robot == 3: print("You Lose!") elif robot == 1: print("You Win!") wins += 1 elif human == 3: if robot == 1: print("You Lose!") elif robot == 2: print("You Win!") wins += 1 else: print("oops") if wins != (pwins + 1): if hwstreak < wstreak: hwstreak = wstreak wstreak = 0 else: wstreak += 1 while True: robot=moves[random.randint(0,2)] human=input("Would you like to use Rock(1), Paper(2), or Scissors(3)\n") pwins = wins if human == "stop": print("You won", wins, "time(s) out of", games, "games!") print("Your highest win streak is:", hwstreak) print("Your current win streak is:", wstreak) print("You tied", ties, "time(s)") print((wins/games)*100, "% winrate") break elif human != "stop": print("Game", games+1) print("You played:", moveN[int(human)-1]) print("Mr.Robot plays:", moveN[robot-1]) games += 1 checkplay(human, robot)
import random
win = 0
loss = 0
draw = 0
choice = 0
while choice != 'STOP':
x = random.randint(1,3)
print('So,which one do you choose?Rock,paper,or scissors?')
choice = str(input())
if choice == 'STOP' or choice == 'Stop' or choice == 'stop':
break
print('You chose:' + choice)
print('Then take that!')
if x == 1:
print('I chose rock!')
if choice == 'rock' or choice == 'Rock':
print('You...Drawed with me...')
draw += 1
elif choice == 'paper' or choice == 'Paper':
print('Awwww!You won!')
win += 1
elif choice == 'scissors' or choice == 'Scissors':
print('Hahaha!You lost!The best day of my life!')
loss += 1
if x == 2:
print('I chose paper!')
if choice == 'rock' or choice == 'Rock':
print('Hahaha!You lost!The best day of my life!')
loss += 1
elif choice == 'paper' or choice =='Paper':
print('You...Drawed with me...')
draw += 1
elif choice == 'scissors' or choice =='Scissors':
print('Awww!You won!’)
win += 1
if x == 3:
print('I chose scissors!')
if choice == 'rock' or choice == 'Rock':
print('Awwww!You won!')
win += 1
elif choice == 'paper' or choice == 'Paper':
print('Hahaha!You lost!The best day of my life!')
loss += 1
elif choice == 'scissors' or choice == 'Scissors':
print('You...Drawed with me...')
draw += 1
print('You want to stop?Why?F-i-n-e!!!Here you go...')
print(f'So,you won {win} time(s) against me, lost {loss} time(s), and drawed {draw}.')
import random win = 0 loss = 0 draw = 0 choice = 0 while choice != 'STOP': x = random.randint(1,3) print('Do you choose rock, paper, or scissors?') choice = str(input()) if choice == 'STOP' or choice == 'Stop' or choice == 'stop': break print('You have chosen: ' + choice) if x == 1: print('The computer chose Rock') if choice == 'rock' or choice == 'Rock': print('it was a draw') draw += 1 elif choice == 'paper' or choice == 'Paper': print('you win!') win += 1 elif choice == 'scissors' or choice == 'Scissors': print('you lose!') loss += 1 if x == 2: print('The computer chose paper') if choice == 'rock' or choice == 'Rock': print('you lose!') loss += 1 elif choice == 'paper' or choice =='Paper': print('it was a draw') draw += 1 elif choice == 'scissors' or choice =='Scissors': print('you win!') win += 1 if x == 3: print('The computer chose scissors') if choice == 'rock' or choice == 'Rock': print('you won!') win += 1 elif choice == 'paper' or choice == 'Paper': print('you lose!') loss += 1 elif choice == 'scissors' or choice == 'Scissors': print('it was a draw') draw += 1 print('that\'s it!') print(f'And by the way, you got {win} win(s), {loss} loss(es), and {draw} draw(s).')