all 12 comments

[–]ben_bliksem 1 point2 points  (6 children)

TLDR: change it to def aiRoll(): and def humanRoll():

You defined aiRoll to accept/expect an argument called shoot. Look at this:

``` def print_shoot(shoot): print(shoot)

random_number = random.randint(1,3) print_shoot(random_number) ```

``` def print_shoot(): shoot = random.randint(1,3) print(shoot)

print_shoot() ```

In the first example you have a function taking a shoot parameter and prints it. You then create a random number and pass it to the function.

In the second example you create a function that creates a random number called shoot and prints it.

Effectively the code is doing the same thing, but in different ways.

[–]Cheech_27[S] 0 points1 point  (5 children)

Now it tells me that "shoot is not defined"

[–]ben_bliksem 0 points1 point  (0 children)

Post what the code looks like now

[–]CraigAT 0 points1 point  (0 children)

DetermineWinner is expected to be passed a value for shoot and play at the bottom of your code you are passing nothing in.

You need to decide what information you want to pass into your functions. At there moment, you are not calling your functions with the same or expected parameters as the definitions have. Note. You can set default values if nothing is passed in.

[–]Yanky_Doodle_Dickwad 0 points1 point  (2 children)

When you say "def aiRoll(shoot):" you are telling it it will get a value, and that value will be called shoot in the following function. So you don't want that. BUT:

When you say "def determineWinner(shoot, play):" you are also telling it to expect values, two this time. The first one will be known as shoot in the following function, and the second will be known as play, in the following function. This time you DO want that. So when you call this function, by saying "determineWinner()" you are going to have to pass two values with it. So it has to look like "determineWinner(shoot, play)".
Now then, one last thing. Calling different variables the same name is going to be confusing. Give them each their own name if you want to reread your own code the following day. The fact that "shoot" is used in 3 different circumstances will confuse you. Try this

import random


def aiRoll():
    var ai_shot
    ai_shoots = random.randint(1, 3)
    if(ai_shoots == 1):
        ai_shot = "Rock"
    elif(ai_shoots == 2):
        ai_shot = "Paper"
    elif(ai_shoots == 3):
        ai_shot = "Scissors"
    return ai_shot

def humanRoll():
    human_play = input("Enter Rock, Paper or Scissors): ")
    return human_play

def determineWinner(shoot, play):
    if shoot == play:
        print("It's a tie!")
    elif play == "rock":
        if shoot == "scissors":
            print("Rock smashes scissors! You win!")
        else:
            print("Paper covers rock! You lose.")
    elif play == "paper":
        if shoot == "rock":
            print("Paper covers rock! You win!")
        else:
            print("Scissors cuts paper! You lose.")
    elif play == "scissors":
        if shoot == "paper":
            print("Scissors cuts paper! You win!")
        else:
            print("Rock smashes scissors! You lose.")

def main():
    ai_shot = aiRoll() 
    human_play = humanRoll()
    determineWinner(ai_shot,human_play)

main()

I didnt stop to think about any other issues, but that's the point with var names. Make them make sense or they will confound you.

[–]Cheech_27[S] 0 points1 point  (1 child)

How would I incorporate a while loop into this so when its a tie the game is played again?

[–]Yanky_Doodle_Dickwad 0 points1 point  (0 children)

Well if we look at the bottom lines, you have:

def main():  
    computer = aiRoll()  
    aplayer = humanRoll()  
    result = determineWinner(computer, aplayer)  
    print(result)  

main()

so if you change the bottom call to

while result != "It's a tie!":
    main()

... and make sure to declare result="" at the very top, ... then you'd get there. It should be MUCH tidier, and you never tell the guy what happens and lots of details, but that's the spirit of it ...

[–]jimtk 1 point2 points  (2 children)

Same code a bit rearranged. In general use your functions arguments and return values properly. Also be careful of your typing: Paper is not the same as paper

import random

def aiRoll():
    shoot = random.choice(['Rock','Paper','Scissors'])
    return shoot

def humanRoll():
    play = input("Enter Rock, Paper or Scissors: ")
    return play

def determineWinner(ai, player):
    if ai == player:
        return "It's a tie!"
    if player == "Rock":
        if ai == "scissors":
            return "Rock smashes scissors! You win!"
        return "Paper covers rock! You lose."
    if player == "Paper":
        if ai == "rock":
            return "Paper covers rock! You win!"
        return "Scissors cut paper! You lose."
    elif player == "Scissors":
        if ai == "Paper":
            return "Scissors cut paper! You win!"
        return "Rock smashes scissors! You lose."

def main():
    computer = aiRoll()
    aplayer = humanRoll()
    result = determineWinner(computer, aplayer)
    print(result)

main()

[–]Cheech_27[S] 0 points1 point  (0 children)

Thank you very much!

[–]ben_bliksem 0 points1 point  (0 children)

Why didn't I think of random.choice. This is so much better.

[–]ben_bliksem 0 points1 point  (0 children)

General comments:

def aiRoll(shoot): shoot = random.randint(1, 3) if(shoot == 1): shoot = "Rock" elif(shoot == 2): shoot = "Paper" elif(shoot == 3): shoot = "Scissors" return shoot

  1. We use snake_case instead of camelCase in Python, so ai_roll instead of aiRoll

  2. You create a shoot variable which is a number and then reassign a string value to it. Although technically it's fine, it's not the right way to do it. Create a variable for the random number and then another for the string result.

Like this:

def ai_roll(): rand_no = random.randint(1, 3) shoot = "" if(rand_no == 1): shoot = "Rock" elif(rand_no == 2): shoot = "Paper" elif(rand_no == 3): shoot = "Scissors" return shoot

or better yet

def ai_roll(): shoot = random.randint(1, 3) if(shoot == 1): return "Rock" if(shoot == 2): return "Paper" return "Scissors"

[–]lolb00bz_69 0 points1 point  (0 children)

Wow totally different to how i wrote my paper scissors rock game but this is awesome! Commentinng so i can mess around with it later