0

Everytime I run my code only the 2nd boolean is run even though it should test for all options then run that one. When I run this code:

import random
Player_Score = 0
Computer_Score = 0
while Player_Score < 5 or Computer_Score < 5:
    Player_object = input("Would you like to choose R, P, or S?")
    Computer_object = random.sample(set(["R", "P", "S"]), 1)
    if Player_object == "R" or "r":
        if Computer_object == "R":
             print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have tied with the Computer and neither of you have scored a point.")
        elif Computer_object == "P":
            Computer_Score = Computer_Score + 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ". You have been beaten by the Computer and it has scored a point.")
        else:
            Player_Score == Player_Score + 1
            print("You have chosen " +Player_object+ " and the Computer chose " +str(Computer_object)+ ".You have beaten the Computer scored a point.")

I end up with results like:

You have chosen  R and the Computer chose ['S'].You have beaten the Computer scored a point.
Would you like to choose R, P, or S?R
You have chosen R and the Computer chose ['P'].You have beaten the Computer scored a point.
Would you like to choose R, P, or S?R
You have chosen R and the Computer chose ['S'].You have beaten the Computer scored a point.
Would you like to choose R, P, or S?R
You have chosen R and the Computer chose ['R'].You have beaten the Computer scored a point.
Would you like to choose R, P, or S?R
You have chosen R and the Computer chose ['P'].You have beaten the Computer scored a point.

So is there a way to fix this, because at this point I don't know what to do to fix this.

Joe Bird
  • 21
  • 4
  • Try `Player_object == "R" or Player_Object == "r"`. Alternatively you could use `Player_object.lower() == "r"`. – PhillipD Nov 14 '17 at 14:21
  • `Player_object == "R" or "r"` doesn't mean what you think it means – John Coleman Nov 14 '17 at 14:21
  • `if Player_object == "R" or "r"` is not the right way to test for multiple values. – John Gordon Nov 14 '17 at 14:21
  • if i remember right, python will use the same `random` when it runs until something about it changes – mast3rd3mon Nov 14 '17 at 14:22
  • Aren't all your options identical ? – Mad Physicist Nov 14 '17 at 14:22
  • 2
    @mast3rd3mon that is neither accurate nor relevant – John Coleman Nov 14 '17 at 14:23
  • 3
    You also have an issue with using `random.sample()`. It returns a *list*, not a single object, which is why you see `['P']` and not `P` in your output. To pick a single option at random out of a sequence, use `random.choice()` instead. – Martijn Pieters Nov 14 '17 at 14:23
  • @MadPhysicist: no, the ends of each printed string differs. – Martijn Pieters Nov 14 '17 at 14:24
  • @JohnColeman how is it? the random will be changed only once so the `Computer_object` value wont change, hence hitting the second block every time – mast3rd3mon Nov 14 '17 at 14:25
  • One more thing - `Player_Score == Player_Score + 1` is not doing assignment – OneCricketeer Nov 14 '17 at 14:25
  • @mast3rd3mon `Computer_object = random.sample(set(["R", "P", "S"]), 1)` will return a different random sample each time it is executed. `random` is a module which retains its own internal state, it isn't an object that has to be continually recreated. You seem to be thinking more in terms of something like Java (unless I misunderstood your comment). – John Coleman Nov 14 '17 at 14:29
  • @JohnColeman unless it changed, i remember python not using a different `random` number everytime i used it, it would pick 1 and use it all the time until i changed part of the `random` statement – mast3rd3mon Nov 14 '17 at 14:32
  • @mast3rd3mon Simple test: run `for _ in range(5): print(random.sample(set(["R", "P", "S"]), 1),end = ", ")`. You should see something like: `['R'], ['P'], ['P'], ['S'], ['S'], ` – John Coleman Nov 14 '17 at 14:35
  • As Martijn Pieters states `Computer_object` is a list with one element. That's why this works: `if Computer_object[0] == "R":` – Pygmalion Nov 14 '17 at 14:41

0 Answers0