jeudi 27 octobre 2016

Python Elif Comparison Not Working [duplicate]

This question already has an answer here:

Rookie question here. I'm trying to make a rock paper scissors game for a school project but the comparison part of my code is broken so that it outputs I win no matter what the score is. The comparison seems to be completely ignoring the actual value of the score. For example, it could by -1 and i would still win.

Below is the code:

from random import randint
def calc():
    if comp==1:
        numc="rock";
    elif comp==2:
        numc="paper";
    else:
        numc="scissors"


    score=num-comp
    if score == -2 or 1:
        print("It looks like the computer chose "+numc+" so you win!");
    elif score == -1 or 2:
        print("It looks like the computer chose "+numc+" so you lost!");
    elif score == 0:
        print("It looks like the computer chose"+numc+ "so it's a tie!");
    print(score)


def start():
    global comp
    global player
    global num
    comp=randint(1,3)
    print("Type R for rock, P for paper, S for scissors.")
    player=str(input("Rock, paper, scissors GO! What do you choose? ")).upper()
    if player=="R":
        num=1
    elif player=="P":
        num=2
    elif player=="S":
         num=3
    else:
        print("Invalid input. Try again.")
        start()

    calc()

start()


#1 for stone, 2 for paper and 3 for scissors

Aucun commentaire:

Enregistrer un commentaire