Bottom Page

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()

Quote
#2
  • Your randomization isn't working because randint returns an int, and you are comparing that to strings.
  • Better would be random.choice('rps'), it just gives you what you need in one line.
  • Globals lead to confusing and hard to maintain code. Pass parameters and assign return values. See the function tutorial for more details.
  • For multiple comparisons in is often [the best choice: if rps not in ('r', 'p', 's'):.
  • RPS is a great place to learn dictionaries instead of complicated if/elif/else:

wins = {'r': 's', 'p': 'r', 's': 'p'}
if rps = ai:
    print('tie')
elif wins[ai] == rps:
    print('win')
else:
    print('loss')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#3
(Sep-13-2019, 12:40 PM)ichabod801 Wrote:
  • Your randomization isn't working because randint returns an int, and you are comparing that to strings.
  • Better would be random.choice('rps'), it just gives you what you need in one line.
  • Globals lead to confusing and hard to maintain code. Pass parameters and assign return values. See the function tutorial for more details.
  • For multiple comparisons in is often [the best choice: if rps not in ('r', 'p', 's'):.
  • RPS is a great place to learn dictionaries instead of complicated if/elif/else:
 wins = {'r': 's', 'p': 'r', 's': 'p'} if rps = ai: print('tie') elif wins[ai] == rps: print('win') else: print('loss') 

Hello sir!
You said I was comparing an integer to a string, and that was causing problem. This has happened with me before. I'm almost too new, could you please tell how to compare integer to integer? Please just send me the correction of that part, since I don't know how to do any other corrections you told. Thank you!
Quote
#4
You just use integer literals (without quotes) instead of string literals (with quotes):

        cpu = randint(1, 3)
 
        ai = cpu
 
        if cpu == 1:
            ai = "r"
 
        elif cpu == 2:
            ai = "p"
 
        elif cpu == 3:
            ai = "s"
But, note that random.choice will pick a random item out of a sequence, even a string. So all of the above code could be replaced with this one line:

ai = random.choice('rps')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Rock, Paper, Scissors Help jyou99 1 460 Mar-26-2018, 04:07 PM
Last Post: nilamo
  basic code help chche5 4 1,403 Feb-17-2018, 10:24 PM
Last Post: chche5
  Rock Paper Scissors (Need help) Lukili 1 643 Jan-14-2018, 10:00 PM
Last Post: Gribouillis
  Basic Doubt in code prateek3 6 1,399 Aug-19-2017, 01:05 PM
Last Post: ichabod801
  Rock, Paper, scissors OmarBrikaa 5 1,663 Jul-11-2017, 09:09 AM
Last Post: OmarBrikaa
  Incredibly basic coding problem AndyF 12 2,907 Feb-11-2017, 03:52 PM
Last Post: AndyF
  Need help with basic code Random 6 1,899 Feb-08-2017, 04:56 AM
Last Post: tannishpage
  Basic code help crawlinkingsnake 2 1,284 Jan-16-2017, 11:03 PM
Last Post: micseydel

Forum Jump:


Users browsing this thread: 1 Guest(s)