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
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)