-2

I am making a simple rock paper scissors game. I am trying to make the user input again if his input isn't rock, paper and scissors. I tried using just else/if/elif but it didn't work. I then tried try and except but it still didn't work.

Here is the code:

Main.py

import random

def rps():
 while True:
     uw = 0 #user wins
     cw = 0 #computer wins

     option = ["rock", "paper", "scissors"] #option so the computer can choose from

     rcn = random.randint(0, 2) #random int variable
     cnp = option[rcn] #random computer option using random int variable
    
     ui = None

     print("Welcome to Rock Paper Scissors!") #start
     print("")
     pon = input("Would you like to play Yes/No? ")

     if pon.lower == "No":
        break #closing if doesnt want
     elif pon.lower() == "Yes":     
      continue

     ui = input("Select rock, paper or scissors: ")
    
     if ui == "rock" and cnp == "scissors":
      print("You won! Computer picked", cnp + ".")
      uw += 1

     elif ui == "rock" and cnp == "paper":
             print("You lost! Computer picked", cnp + ".")
             cw += 1

     elif ui == "rock" and cnp == "rock":
      print("Draw! Computer picked", cnp + ".")

     elif ui == "scissors" and cnp == "paper":
       print("You won! Computer picked", cnp + ".")
       uw += 1

     elif ui == "scissors" and cnp == "rock":
         print("You lost! Computer picked", cnp + ".")
         cw += 1

     elif ui == "scissors" and cnp == "scissors":
         print("Draw! Computer picked", cnp + ".")

     elif ui == "paper" and cnp == "rock":
         print("You won! Computer picked", cnp + ".")
         uw += 1

     elif ui == "paper" and cnp == "scissors":
         print("You lost! Computer picked", cnp + ".")
         cw += 1
    
     elif ui == "paper" and cnp == "paper":
          print("Draw! Computer picked", cnp + ".")
    
     else:
         print("Invalid input. Try again!")
         ui = input("Select rock, paper or scissors: ")
    # i also tried this one but still
    #else: 
      #   try:
     #         print("Invalid input. Try again!")
       #       ui = input("Select rock, paper or scissors: ")
      #   except ValueError:
        #       print("Invalid input. Try again!")
        #       ui = input("Select rock, paper or scissors: ")
         

    
     c = input("Would you like to play again Yes/No: ")
     if c == "No":
         print("")
         print("Your wins", uw)
         print("Computer wins", cw)
         b = input("Would you like to close the game Yes/No? ")
         if b.lower() == "Yes":
              break
         elif b.lower() == "No":
              continue
     elif c == "Yes":
           continue

rps()

If somebody could help me i would be thankful!

  • 1
    Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – jjramsey Jan 21 '22 at 16:36
  • Just an observation `if pon.lower == "No":` will never be True. There are no `()` after the `lower` so it's checks equality between built-in method of lower and 'No'. If this is fixed (by adding parentheses) then it also can't never be True (on one side there is lowercase word and on another 'No'). Latter applies also to elif clause. – Aivar Paalberg Jan 21 '22 at 16:39
  • @AivarPaalberg changed it, it was a problem but didnt solve my main problem. Thanks for the observation though. – ItsMeDataForAWhile Jan 21 '22 at 16:46
  • @jjramsey possibly, i wll read more on it and get the hang of his answer. Ty for the link! – ItsMeDataForAWhile Jan 21 '22 at 16:48
  • @ItsMeDataForAWhile could you read my answer please, so i can improve it? – XxJames07- Jan 21 '22 at 16:54

2 Answers2

0

PS. You have to pip install pandas for this to work. The thing to ask to exit was annoying if you wanted to play for like 25 plays, so i removed it and replaced it with when you pick 'exit' as the chosen thing:

import random,pandas as pd
def rps():
    uw = 0 #user wins
    cw = 0 #computer wins
    option = {"rock":"scissors", "paper":"rock", "scissors":"paper"} #option so the computer can choose from
    ltw = "cw"
    dataframe = pd.DataFrame({
        "Wins":[uw,cw],"Last to Win":[ltw=='uw',ltw=="cw"]
    },
    index=["User","Computer"]
    )
    ci = random.choice(list(option.keys())) #you can use random.choice instead
    # ui = None unneccessary
    while True:


        print("Welcome to Rock Paper Scissors!\n") #start
        pon = input("Would you like to play Yes/No? ")

        if pon.lower() == "no":
           break #closing if doesnt want
        ui = input("Select rock, paper or scissors: ")
        if ui == ci:
            print("DRAW!")
        elif ui == option[ci]:
            print("Computer Wins!")
            dataframe["Wins"][1]+=1
            print(dataframe)
        elif option[ui] == ci:
            print("User Wins!")
            dataframe["Wins"][0]+=1
            print(dataframe)
        if pon == "exit":
            print("Results:\n",dataframe)
            b=input("Would you like to close the game Yes/No? ")
            if b.lower() == "yes":break
            else:continue
        if pon not in option:
            print("NOT A VALID OPTION!")
rps()
XxJames07-
  • 1,833
  • 1
  • 4
  • 17
  • This didnt really change anything. I am trying to modify my code so it does this: Ask if the user wants to play -> if the user wants to play ask rock paper or scisors then do the rock paper scissors commands then ask him if he wants to play again, if he wants i want to reset the code back to the asking rock paper scissors part. If he says no i want it to print out computer wins and user wins. If a invalid number is put in rock paper scissors i want it to like print the input again. I dont know if my code is correct. i am trying to fix it but nothing. – ItsMeDataForAWhile Jan 21 '22 at 21:14
  • this is the right answer but if you want something a little bit more compact you couldanswer me in this post, if you haven't checked my code works perfectly, so try it and reply with a coherent response. – XxJames07- Jan 22 '22 at 11:45
0

This is to address little part of the code - how to find the winner. Instead of long comparison block one can approach from another angle - what are the winning combinations. As it turns out there are only three of them (first beats second):

first_wins = [('Rock', 'Scissors'), ('Paper', 'Rock'), ('Scissors', 'Paper')]

Now, if we have computer and player choices we can just write:

if player_choice == computer_choice:
    # draw
elif (player_choice, computer_choice) in first_wins:
    # player wins
else:                   # if no draw and no first wins
    # computer wins
Aivar Paalberg
  • 4,645
  • 4
  • 16
  • 17