Python Forum
Rock Paper Scissors (Need help)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rock Paper Scissors (Need help)
#1
So i've been trying to figure out what is wrong here but i think that i dont have enough experience in python yet.
Can anyone point out my mistake here?
def main():
    choice = input("Pick one: 1)Rock 2)Paper 3)Scissors | Answer: ")
    if (choice == "1"):
        print (">You chose rock!<")
    if (choice == "2"):
        print (">You chose paper!<")
    if (choice == "3"):
        print (">You chose scissors!<")
    answer ()
 
def mid():
    import time
    time.sleep(2)
    import random
    botc = random.randint(1,3)
 
def answer():
    if (botc == "1" and choice =="1"):
        print("Picked Rock!Tie!")
    if (botc == "2" and choice == "2"):
        print ("Picked Paper!Tie!")
    if (botc == "3" and choice == "3"):
        print ("Picked scissors!Tie!")
    if (botc == "1" and choice == "2"):
        print ("Picked rock!You win!")
    if (botc == "1" and choice == "3"):
        print ("Picked rock!You lost!")
    if (botc == "2" and choice == "1"):
        print ("Picked paper!You lose!")
    if (botc == "2" and choice == "3"):
        print ("Picked paper!You win!")
    if (botc == "3" and choice == "1"):
        print ("Picked scissors!You win!")
    if (botc == "3" and choice == "2" ):
        print ("Picked scissors!You win!")
    mid()
 
main()
Thanks
Reply
#2
You want to share the values of choice and botc between the three functions. The way things are written, every function has its own variables choice and botc. A solution is to use global variables. You can write
choice = None
botc = None
at the top of the file, then you can start function main with a global statement
def main():
    global choice
    ...
This tells python that it is the same global variable in the function. In the same way, you need to write
def mid():
    global botc
    ...
and
def answer():
    global choice, botc
    ...
Shouldn't mid() be called at the beginning of answer() ?

Later you will learn that there are better ways to share variables between functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Rock Paper Scissors Project in Python ankitdixit 8 4,728 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,786 Mar-01-2022, 03:20 PM
Last Post: Agat0
  Problem restricting user input in my rock paper scissors game ashergreen 6 4,503 Mar-25-2021, 03:54 AM
Last Post: deanhystad
  Odd behavior with Rock Paper Scissor game DustinKlent 2 1,888 Aug-27-2020, 03:55 PM
Last Post: DustinKlent
  Trying to implement Best of 3 logic in rock paper scissors game ShAhCh 5 3,268 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,606 Mar-19-2020, 02:46 AM
Last Post: jefsummers
  Problem with Basic Rock Paper Scissors code BirinderSingh 3 2,388 Sep-13-2019, 03:28 PM
Last Post: ichabod801
  Rock, Paper, Scissors Help jyou99 1 2,413 Mar-26-2018, 04:07 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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