Python Forum
Problem with Basic Rock Paper Scissors code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Basic Rock Paper Scissors code
#1
Guys I just started with python a couple days back, and today finally sat down to write a code for Rock Paper Scissors.

The code abruptly ends after just executing game() function. Many other codes I checked closely resembled mine, but they worked and my doesn't. The problem seems to be with the Randomization, since in my testing everything works except for it. Any help would be greatly appreciated. I know the code is a bit too long for such a simple game, but i don't need to know how to shorten it (will save it for later), just what is causing the problem right now.

Thanks

from os import system, name

from time import sleep

from random import randint

rps = None

cpu = None

ai = None


def strt():
    print("Rock Paper Scissors")

    sleep(1)


def game():

    global rps

    global cpu

    global ai

    rps = input("Choose Rock (r), Paper (p), or Scissors (s)")

    if rps != "r" and rps != "p" and rps != "s":
        print("Invalid function, starting over...")
        sleep(1)
        game()

    else:

        cpu = randint(1, 3)

        ai = cpu

        if cpu == "1":
            ai = "r"

        elif cpu == "2":
            ai = "p"

        elif cpu == "3":
            ai = "s"

        decision()


def decision():
    if rps == ai:
        print("Decision Neutral")
        game()

    elif rps == "r":
        if ai == "s":
            print("you win")
        elif ai == "p":
            print("you lose")

    elif rps == "p":
        if ai == "s":
            print("you lose")
        elif ai == "r":
            print("you win")

    elif rps == "s":
        if ai == "r":
            print("you lose")
        elif ai == "p":
            print("you win")


strt()

game()
Reply


Messages In This Thread
Problem with Basic Rock Paper Scissors code - by BirinderSingh - Sep-13-2019, 12:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,717 Feb-23-2024, 03:14 PM
Last Post: DPaul
  Trying to create a visual rock paper scissors game urmom33 1 976 Dec-03-2022, 09:12 PM
Last Post: deanhystad
  Rock paper scissors in python with "algorithm" Agat0 23 5,777 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,492 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Odd behavior with Rock Paper Scissor game DustinKlent 2 1,886 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  List index out of range error when attempting to make a basic shift code djwilson0495 4 2,933 Aug-16-2020, 08:56 PM
Last Post: deanhystad
  Writing a basic shift code djwilson0495 2 2,218 Aug-16-2020, 01:52 PM
Last Post: djwilson0495
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 3,265 May-11-2020, 04:31 PM
Last Post: ShAhCh
  Rock Paper Scissors with dictionaries ewgreht 2 3,823 May-01-2020, 03:19 PM
Last Post: deanhystad
  Rock, Paper, Scissors.. Help..hidden bug xxunknownxx 4 2,605 Mar-19-2020, 02:46 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020