-2

I know that there are more clever and short answers than this, but it's not printing the conditions that I give it. Do you have solutions for this? And how can I use if statements in loops?

import random
game = input("Do you want to play the game? yes/no")

for x in range(1):
    pc_input = random.randint(1,3)

rock = 1
paper = 2
scissors = 3

if game == "yes":
   usr_input = input("Rock , paper , scissors ")

   if pc_input == 1 and usr_input == 1:
       print("Tied game...")

   if pc_input == 1 and usr_input == 2:
       print("You won")

   if pc_input == 1 and usr_input == 3:
       print("You lost")

   if pc_input == 2 and usr_input == 1:
       print("You lost")

   if pc_input == 2 and usr_input == 2:
       print("Tied game...")

   if pc_input == 2 and usr_input == 3:
       print("You won")

   if pc_input == 3 and usr_input == 1:
       print("You won")

   if pc_input == 3 and usr_input == 2:
       print("You lost")

   if pc_input == 3 and usr_input == 3:
       print("Tied game...")
wjandrea
  • 23,210
  • 7
  • 49
  • 68
DinosaurT
  • 21
  • 1
  • 6
  • 4
    write a dictionary mapping the two inputs to the outputs e.g. `{(1, 1): 'Tied game'}` etc. – Chris_Rands Nov 17 '19 at 19:05
  • 1
    Possible duplicate of [How can I read inputs as numbers?](https://stackoverflow.com/questions/20449427/how-can-i-read-inputs-as-numbers) – Sayse Nov 17 '19 at 19:05
  • 1
    `usr_input`s are all strings – Sayse Nov 17 '19 at 19:05
  • 1
    ```usr_input = int(input("Rock , paper , scissors "))``` – Number70 Nov 17 '19 at 19:06
  • 2
    why you use loop when generating a random number. You are generating only one random number. – Sheri Nov 17 '19 at 19:08
  • There are lots of things wrong with this code, and you're asking more than one question, so I'm voting to close this question as "too broad". Please make a [mre] of one particular problem. See [ask] for more advice. – wjandrea Nov 17 '19 at 19:12

3 Answers3

0

Try this with a simple dictionary with all possible winning and tie cases and loss cases otherwise :

import random
game = input("Do you want to play the game? yes/no"  )

for x in range (1):
    pc_input = random.randint(1,3)

rock = 1
paper = 2
scissors = 3
status_dict = {(1,1): 'Tied', (2,2): 'Tied', (3,3): 'Tied', (1,2): 'You won', (2,3): 'You won', (3,1): 'You won'}

if game == "yes":
   usr_input = int(input("Rock , paper , scissors "))
   result = status_dict.get((pc_input, usr_input), 'You lost')
   print(result)
Arkistarvh Kltzuonstev
  • 6,572
  • 7
  • 24
  • 47
  • This is a strange way of setting up the dictionary; what you want is to use the pair `(pc_input, usr_input)` to look up the result, so it would make more sense to have a dictionary like `{(1,1): 'Tied', (2,2): 'Tied', (3,3): 'Tied', (1,2): 'You won', (2,3): 'You won', (3,1): 'You won'}`. That way you can write `v = status_dict.get((pc_input, usr_input), 'You lost')` with no loop or `else`. – kaya3 Nov 17 '19 at 19:44
  • @kaya3 Updated with your suggestion. – Arkistarvh Kltzuonstev Nov 17 '19 at 20:50
0

There are only three different behaviours, so try to write an if/elif chain with only three branches. You can simplify the logic to check more options in the same condition:

diff = (pc_input - usr_input) % 3

if diff == 0:
    print("Tied game...")

elif diff == 1:
    print("You lost")

elif diff == 2:
    print("You won")

diff is the difference between the two numbers modulo 3, i.e. if you think of 1, 2, 3 as a cycle (like rock, paper, scissors) then the difference modulo 3 tells you how far "ahead" pc_input is in the cycle, compared to usr_input. Being "ahead" by 2 is the same as being "behind" by 1.

kaya3
  • 41,043
  • 4
  • 50
  • 79
-1

You can nest your if statements so you don't need so much repetition:

if pc_input == usr_input:
    print("Tied game...")
elif pc_input == 1:
    if usr_input == 2:
        print("You won")
    else:
        print("You lost")
elif pc_input == 2:
    if usr_input == 3:
        print("You won")
    else:
        print("You lost")
else:
    if usr_input == 1:
        print("You won")
    else:
        print("You lost")

You also need to convert the user's input to an integer before comparing. And the prompt should tell them what the numbers are.

usr_input = int(input("1: Rock , 2: paper , 3: scissors "))
Barmar
  • 669,327
  • 51
  • 454
  • 560