mardi 27 juin 2017

Python Program Ignoring If Statement?

I've been using python at school for a while now.

We are currently working on a workbook where the question is about creating a game for rock, paper, scissors. I have tried several different versions of the code now, but no matter what I have done one if statement in the code has been ignored. I have either got an error saying that a variable is not defined (which IS defined within an if statement) or no error is registered and the program just ignores an if statement which is the case within this new version.

import time
answer = input("Would you like to play rock, paper, scissors? ")
time.sleep(0.5)
if answer[0].lower() == "y":
    name = input("What is your name? ")
    print("Hello", name, "good luck.")
    time.sleep(1)
    rounds = int(input("How many rounds would you like to play? This must be between 3 and 10. "))
    while rounds < 3:
        rounds = int(input("Please enter a number between 3 and 10. "))
    while rounds > 10:
        rounds = int(input("Please enter a number between 3 and 10. "))
        while rounds < 3:
            rounds = int(input("Please enter a number between 3 and 10. "))
    count = 0
    from random import randint
    while count < rounds:
        rps = input("Rock, paper, or scissors? ")
        randomRPS = randint(1,3)
        print("Rock...")
        time.sleep(0.5)
        print("Paper...")
        time.sleep(0.5)
        if randomRPS == 1:
            letterRPS = "Rock!"
            print(letterRPS)
        if randomRPS == 2:
            letterRPS = "Paper!"
            print(letterRPS)
        if randomRPS == 3:
            letterRPS = "Scissors!"
            print(letterRPS)
        if rps[0].lower == "r":
            if letterRPS[0].lower == "r":
                print("Draw!")
            elif letterRPS[0].lower == "p":
                print("You lose!")
            else:
                print("You win!")
        elif rps[0].lower == "p":
            if letterRPS[0].lower == "p":
                print("Draw!")
            elif letterRPS[0].lower == "s":
                print("You lose!")
            else:
                print("You win!")
        elif rps[0].lower == "s":
            if letterRPS[0].lower == "s":
                print("Draw!")
            elif letterRPS[0].lower == "r":
                print("You lose!")
            else:
                print("You win!")

        count += 1

The problematic code:

        if rps[0].lower == "r":
            if letterRPS[0].lower == "r":
                print("Draw!")
            elif letterRPS[0].lower == "p":
                print("You lose!")
            else:
                print("You win!")
        elif rps[0].lower == "p":
            if letterRPS[0].lower == "p":
                print("Draw!")
            elif letterRPS[0].lower == "s":
                print("You lose!")
            else:
                print("You win!")
        elif rps[0].lower == "s":
            if letterRPS[0].lower == "s":
                print("Draw!")
            elif letterRPS[0].lower == "r":
                print("You lose!")
            else:
                print("You win!")

Aucun commentaire:

Enregistrer un commentaire