geekcomputers / Python

My Python Examples
http://www.thegeekblog.co.uk
MIT License
31.18k stars 12.19k forks source link

I am stuck with my Rock, Paper, Scissors python game #688

Open Krazy0999 opened 4 years ago

Krazy0999 commented 4 years ago

I am new to GitHub so if I have posted this help request in the wrong place please let me know. I am trying to make rock, paper, scissors in python without googling the answer and I have come across an issue. I am playing around with it and the only option the computer is picking is Rock. It will not pick paper or scissors, no matter what the user inputs. Yesterday it was working fine and the computer would randomly pick something but now it only picks the number 1 or Rock, can anyone see why this is happening? Have I put some code in wrong which is causing this to happen? All help is grateful. This is my code, the def main(): and while True have not been added to the code area for some reason but in the python code they are indented properly and everything so I do not believe they are the problem.

import time import random def main(): while True:

    computer = random.randint(1,3)
    user = input("Rock, Paper, Scissors, SHOOT!  ")

    if computer == 1 and user == "Rock" or user == "rock":
        print("I pick Rock!")
        time.sleep(0.5)
        print("Tie")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 1 and user == "Paper" or user == "paper":
        print("I pick Rock!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 1 and user == "Scissors" or user == "scissors":
        print("I pick Rock!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")

    elif computer == 2 and user == "Rock" or user == "rock":
        print("I pick Paper!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 2 and user == "Paper" or user == "paper":
        print("I pick Paper!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 2 and user == "Scissors" or user == "scissors":
        print("I pick Paper!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")

    elif computer == 3 and user == "Rock" or user == "rock":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("You win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 3 and user == "Paper" or user == "paper":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("I win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 3 and user == "Scissors" or user == "scissors":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif user == "Finished" or user == "finished":
        print("Okay!")
        break
    else:
        print("Sorry, that isn't an option I understand... yet.")

main()

Leyouz233 commented 4 years ago

I don't know if this request for help is wrong, but I'd be happy to help you.

First I took a look at your code, I tried to run your code on my local machine, and there was no exception, but because the next line of the main() function you defined when the email came was indented, I didn't think it was indented.

The following is the operation result of my machine, if you have any special important discovery, I hope you write back to me again.Because my English is not good, these are translated by the translator, so please forgive me for any improper translation.

I also recommend that you define some functions that will make your while statement more concise.

------------------ 原始邮件 ------------------ 发件人: "nvrton"<notifications@github.com>; 发送时间: 2020年3月7日(星期六) 晚上8:08 收件人: "geekcomputers/Python"<Python@noreply.github.com>; 抄送: "Subscribed"<subscribed@noreply.github.com>; 主题: [geekcomputers/Python] I am stuck with my Rock, Paper, Scissors python game (#688)

I am new to GitHub so if I have posted this help request in the wrong place please let me know. I am trying to make rock, paper, scissors in python without googling the answer and I have come across an issue. I am playing around with it and the only option the computer is picking is Rock. It will not pick paper or scissors, no matter what the user inputs. Yesterday it was working fine and the computer would randomly pick something but now it only picks the number 1 or Rock, can anyone see why this is happening? Have I put some code in wrong which is causing this to happen? All help is grateful. This is my code, the def main(): and while True have no been added to the code area for some reason but in the python code they are indented properly and everything so I do not believe they are the problem.

import time import random def main(): while True: computer = random.randint(1,3) user = input("Rock, Paper, Scissors, SHOOT! ") if computer == 1 and user == "Rock" or user == "rock": print("I pick Rock!") time.sleep(0.5) print("Tie") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 1 and user == "Paper" or user == "paper": print("I pick Rock!") time.sleep(0.5) print("You win") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 1 and user == "Scissors" or user == "scissors": print("I pick Rock!") time.sleep(0.5) print("I win!") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 2 and user == "Rock" or user == "rock": print("I pick Paper!") time.sleep(0.5) print("I win!") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 2 and user == "Paper" or user == "paper": print("I pick Paper!") time.sleep(0.5) print("Tie!") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 2 and user == "Scissors" or user == "scissors": print("I pick Paper!") time.sleep(0.5) print("You win") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 3 and user == "Rock" or user == "rock": print("I pick Scissors!") time.sleep(0.5) print("You win!") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 3 and user == "Paper" or user == "paper": print("I pick Scissors!") time.sleep(0.5) print("I win") time.sleep(0.5) print("Either play again or type Finished to stop!") elif computer == 3 and user == "Scissors" or user == "scissors": print("I pick Scissors!") time.sleep(0.5) print("Tie!") time.sleep(0.5) print("Either play again or type Finished to stop!") elif user == "Finished" or user == "finished": print("Okay!") break else: print("Sorry, that isn't an option I understand... yet.")
main()

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

Krazy0999 commented 4 years ago
Alright, thank you for you help! If I find anything out I will be sure to let you know! Sent from Mail for Windows 10 From: f_方老板Sent: 07 March 2020 13:16To: geekcomputers/PythonCc: nvrton; AuthorSubject: Re: [geekcomputers/Python] I am stuck with my Rock, Paper, Scissors python game (#688) I don't know if this request for help is wrong, but I'd be happy to help you. First I took a look at your code, I tried to run your code on my local machine, and there was no exception, but because the next line of the main() function you defined when the email came was indented, I didn't think it was indented. The following is the operation result of my machine, if you have any special important discovery, I hope you write back to me again.Because my English is not good, these are translated by the translator, so please forgive me for any improper translation. I also recommend that you define some functions that will make your while statement more concise. ------------------ 原始邮件 ------------------ 发件人: "nvrton"
Captain52hz commented 4 years ago

I read your code and want to tell you something about it. I think the following code line is True when your input is rock or Rock if computer == 1 and user == "Rock" or user == "rock": I suggest it could write as following: if computer == 1 and (user == "Rock" or user == "rock"): do you agree? I have poor English and hope help you!

Krazy0999 commented 4 years ago

Ah right okay! Thank you for the tip!

On Tue, 10 Mar 2020, 03:10 Captain, notifications@github.com wrote:

I read your code and want to tell you something about it. I think the following code line is True when your input is rock or Rock if computer == 1 and user == "Rock" or user == "rock": I suggest it could write as following: if computer == 1 and (user == "Rock" or user == "rock"): do you agree? I have poor English and hope help you!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/geekcomputers/Python/issues/688?email_source=notifications&email_token=AMTK56OJWG5W2Q6TLMIWKHTRGWVTJA5CNFSM4LDPEKZKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEOJ2OYQ#issuecomment-596879202, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMTK56I2B4YVNCGFOFOEFDTRGWVTJANCNFSM4LDPEKZA .

codeperfectplus commented 4 years ago

You can use .upper of .lower to reduce program complexity.

Krazy0999 commented 4 years ago

Ah yes, okay, thank you

On Fri, 17 Apr 2020, 09:44 Deepak Raj, notifications@github.com wrote:

You can use .upper of .lower to reduce program complexity.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/geekcomputers/Python/issues/688#issuecomment-615123849, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMTK56PFSXFIY6GIXX7KEFLRNAJINANCNFSM4LDPEKZA .

timush-a commented 4 years ago

Hello. I think that's you can use dictionary for determinate the winner. Also you can use random.choice instead of random.randint. Like this for example:

from random import choice

def computer_choice():
        options = ('rock', 'paper', 'scissors')
        return choice(options)

game_state = {
    'rock-rock': 'Tie',
    'rock-paper': 'You win',
    'rock-scissors: 'Computer win',
    'paper-paper': 'Tie',
    'paper-scissors': 'You win',
    'paper-rock': 'Computer win',
    'scissors-scissors': 'Tie',
    'scissors-rock': 'You win',
    'scissors-paper': 'Computer win'
    }

def game():  
    user_choice = input('Chose rock, paper or scissors ----->').lower()
    comp_choice = computer_choice()
    print(f"You chose {user_choice}, computer chose {comp_choice}")
    print(game_state[f"{comp_choice}-{user_choice}"])

if __name__ == '__main__':
    game()
sandeshavadhani commented 4 years ago

Created one using object oriented programming

import random

class NewGame:

    def __init__(self):
        self.score = 0
        self.human = 0
        self.comp = 0
        self.comp_score = 0
        self.rps_dict = {1: 'rock', 2: 'paper', 3: 'scissor'}

    def calc_score(self, human, comp):
        self.human = human
        self.comp = comp
        scor_tupl = (self.human, self.comp)
        if (3 not in scor_tupl) and (1 not in scor_tupl):
            if self.human > self.comp:
                self.score += 1
            elif self.comp > self.human:
                self.comp_score += 1
        elif (3 in scor_tupl) and (2 in scor_tupl):
            if self.human > self.comp:
                self.score += 1
            elif self.comp > self.human:
                self.comp_score += 1
        else:
            if self.human < self.comp:
                self.score += 1
            elif self.comp < self.human:
                self.comp_score += 1

    def chk_endgame(self):
        if max(self.score, self.comp_score) == 2:
            if self.score > self.comp_score:
                return "Player"
            elif self.comp_score > self.score:
                return "Computer"
            else:
                return "No-one"
        else:
            return None

if __name__ == '__main__':
    game = NewGame()
    while True:
        try:
            print("Enter 1 for rock, 2 for paper, 3 for scissor: ")
            sel_player = int(input())
            if sel_player in game.rps_dict:
                sel_comp = random.choice((1, 2, 3))
                print("You: " + game.rps_dict[sel_player])
                print("Computer: " + game.rps_dict[sel_comp])
                game.calc_score(sel_player, sel_comp)
                if game.chk_endgame() is not None:
                    print("{} wins the game".format(game.chk_endgame()))
                    break
            else:
                break
        except:
            print('Error!')
Kpraful commented 4 years ago

Okh i might be late to party, But i found exactly what the problem is. It due to your if statements since you havent used bracket to seprate and/or condition, so what ever you input is True according to interpreter. Here see this example if computer == 1 and user == "Rock" or user == "rock" if you do this than interpreter is always considering your input as true and not going to another condition. Instead You can use if ((computer == 1) and (user == "Rock" or user == "rock")): It says clearly on what you want to perform and/or operations.

Karim8697 commented 4 years ago

Yeah the issue was the OR statements. User being either rock, paper or scissors was always just triggering one of the first three statements. I solved this and shortened the code by using .lower on the user input and removing the OR statements as below.

import random
import time

computer = random.randint(1,3)
                                        #Added .lower
user = input("Rock, Paper, Scissors, SHOOT!  ").lower()

    # ORIGINAL CODE
#if computer == 1 and user == "Rock" or user == "rock":
#    print("I pick Rock!")             ^---------------------------  
 #   time.sleep(0.5)                                              |
 #   print("Tie")                                                 |
#    time.sleep(0.5)                                              |
#    print("Either play again or type Finished to stop!")         |
#elif computer == 1 and user == "Paper" or user == "paper":       |
#    print("I pick Rock!")                ^-----------------------|
#    time.sleep(0.5)                                              | ----Because the user is going to pick rock, paper or
#    print("You win")                                             |     scissors, one of these first 3 'or' statements will
#    time.sleep(0.5)                                              |     always return 'True'.
#    print("Either play again or type Finished to stop!")         |
#elif computer == 1 and user == "Scissors" or user == "scissors": |
#    print("I pick Rock!")                   ^---------------------
#    time.sleep(0.5)
 #   print("I win!")
#    time.sleep(0.5)
#    print("Either play again or type Finished to stop!")

#Removed the problem 'or' statements, leaving only the lowercase options

if computer == 1 and user == "rock":
    print("I pick Rock!")
    time.sleep(0.5)
    print("Tie")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 1 and user == "paper":
    print("I pick Rock!")
    time.sleep(0.5)
    print("You win")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 1 and user == "scissors":
    print("I pick Rock!")
    time.sleep(0.5)
    print("I win!")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")

elif computer == 2 and user == "rock":
    print("I pick Paper!")
    time.sleep(0.5)
    print("I win!")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 2 and user == "paper":
    print("I pick Paper!")
    time.sleep(0.5)
    print("Tie!")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 2 and user == "scissors":
    print("I pick Paper!")
    time.sleep(0.5)
    print("You win")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")

elif computer == 3 and user == "rock":
    print("I pick Scissors!")
    time.sleep(0.5)
    print("You win!")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 3 and user == "paper":
    print("I pick Scissors!")
    time.sleep(0.5)
    print("I win")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif computer == 3 and user == "scissors":
    print("I pick Scissors!")
    time.sleep(0.5)
    print("Tie!")
    time.sleep(0.5)
    print("Either play again or type Finished to stop!")
elif user == "Finished" or user == "finished":
    print("Okay!")
else:
    print("Sorry, that isn't an option I understand... yet.")
main()
RedPlumDragon commented 3 years ago

@Karim8697 I get this error:

Traceback (most recent call last):
  File "/Users/*user*/Desktop/*filename*.py", line 93, in <module>
    main()
NameError: name 'main' is not defined
Kpraful commented 3 years ago

Yeah because I think @Karim8697 forgot to put the code in main function

import random
import time

computer = random.randint(1,3)
                                        #Added .lower
user = input("Rock, Paper, Scissors, SHOOT!  ").lower()

def main(): #added code inside this function
    if computer == 1 and user == "rock":
        print("I pick Rock!")
        time.sleep(0.5)
        print("Tie")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 1 and user == "paper":
        print("I pick Rock!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 1 and user == "scissors":
        print("I pick Rock!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")

    elif computer == 2 and user == "rock":
        print("I pick Paper!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 2 and user == "paper":
        print("I pick Paper!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 2 and user == "scissors":
        print("I pick Paper!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")

    elif computer == 3 and user == "rock":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("You win!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 3 and user == "paper":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("I win")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif computer == 3 and user == "scissors":
        print("I pick Scissors!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        print("Either play again or type Finished to stop!")
    elif user == "Finished" or user == "finished":
        print("Okay!")
    else:
        print("Sorry, that isn't an option I understand... yet.")
main()
RedPlumDragon commented 3 years ago

Added startswith() and instead of just printing "Either play again or type Finished to stop!" I gave an option to play again or finish.


import random
import time

def main(): #added code inside this function
    # put ALL the code here
    computer = random.randint(1,3)
                                                #Added .lower
    user = input("Rock, Paper, Scissors, SHOOT!  ").lower()

    # added startsiwth and option to play again
    if computer == 1 and user.startswith('r'):
        print("I pick Rock!")
        time.sleep(0.5)
        print("Tie")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 1 and user.startswith('p'):
        print("I pick Rock!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 1 and user.startswith('s'):
        print("I pick Rock!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 2 and user.startswith('r'):
        print("I pick Paper!")
        time.sleep(0.5)
        print("I win!")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 2 and user.startswith('p'):
        print("I pick Paper!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 2 and user.startswith('s'):
        print("I pick Paper!")
        time.sleep(0.5)
        print("You win")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()

    elif computer == 3 and user.startswith('r'):
        print("I pick Scissors!")
        time.sleep(0.5)
        print("You win!")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 3 and user.startswith('p'):
        print("I pick Scissors!")
        time.sleep(0.5)
        print("I win")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    elif computer == 3 and user.startswith('s'):
        print("I pick Scissors!")
        time.sleep(0.5)
        print("Tie!")
        time.sleep(0.5)
        option = input("Do you want to (1) Play again or (2) stop: ")
        if option == '1':
            main()
    else:
        print("Sorry, that isn't an option I understand... yet.")
main()