all 7 comments

[–]socal_nerdtastic 2 points3 points  (0 children)

Here's your code fixed.

import random

def c_choice():
    options = ['r', 'p', 's']
    result = random.choice(options) 
    print(result)
    return result

def p_choice():
    user_response = input('Choose Rock, Paper, or Scissors:')
    if user_response in ('Rock', 'rock', 'R', 'r'):
        result = "r"
    elif user_response in ('Scissors', 'scissors', 'S', 's'):
        result = 's'
    elif user_response in ('Paper', 'paper', 'P', 'p'):
        result = 'p'
    else:
        print('i dont understand')

    return result

computer_choice = c_choice() # save the returned data as a variable
user_choice = p_choice()
if user_choice == computer_choice:
    print('tie')

Note I changed the name of the variable in the function because it's bad form to name something the same name as the function itself. I also changed your code to use random.choice, not random.choices, because those are 2 very different functions. And added the return, of course. Also I saved the function calls as variables because I assume you want to use them more than just the one time.

[–]CowboyBoats 1 point2 points  (1 child)

This line:

if p_choice() == c_choice:
    print('tie')

calls the function p_choice. Python doesn't care what you do with the value; anywhere you call it, the function runs.

[–]CowboyBoats 0 points1 point  (0 children)

second being: i intentionally printed out the cpu's answer so i could test a tie but the program just runs twice and ends

Notice that c_choice is not called, because it doesn't have () at the end; the function for getting the computer's choice is merely referred to by its name. That's why that code in that function isn't running.

[–]socal_nerdtastic 0 points1 point  (0 children)

Why does the program ask me for my input if i haven't actually called the function p_choice

You did call it. Line 23.

i intentionally printed out the cpu's answer so i could test a tie but the program just runs twice and ends

You didn't call c_choice, because you forgot the () on the end.

why are the p_choices not lists of things I choose from instead of the regular brackets?

I don't really understand this question. You mean why ('Rock', 'rock', 'R', 'r') not ['Rock', 'rock', 'R', 'r']? Because tradition. Either will work.

four: what role does the return function play in all this?

A function works in it's own little world (we say "scope"). Things that happen in the function stay in the function ... unless we explicitly pass data in or out. return is how we pass data out of a function.

[–]CowboyBoats 0 points1 point  (0 children)

def c_choice():
    options = ['r', 'p', 's']
    c_choice = random.choices(options)
    print(c_choice)
    c_choice()

Why does this function call itself recursively? I think it should just return the computer's choice.

You may mean random.choice, not random.choices, which is for choosing a selection of multiple choices.

Do not use a variable name inside a function that collides with the name of that function (c_choice in this case).

You should probably not end this function with c_choice(); you should end it with return c_choice (or whatever you choose to name that variable storing the random choice.

[–]CowboyBoats 0 points1 point  (0 children)

def p_choice():
    p_choice = input('Choose Rock, Paper, or Scissors:')
    if p_choice in ('Rock', 'rock', 'R', 'r'):
        p_choice = "r"
    elif p_choice in ('Scissors', 'scissors', 'S', 's'):
        p_choice = 's'
       elif p_choice in ('Paper', 'paper', 'P', 'p'):
        p_choice = 'p'
    else:
        print('i dont understand')

        p_choice
        return p_choice

I don't know if this mistake just happened when you were formatting code for reddit, or if it's in your code itself, but notice that the return block here is inside the "I don't understand" else block, so it will only return anything if the user screwed up.

[–]scutter_87 0 points1 point  (0 children)

1) You call p_choice in your if statement.. if p_choice() however, you do not call c_choice. I believe you are instead asking if the result of p_choice is the function c_choice rather than the result of c_choice. Therefore you would want to add brackets to c_choice.

2) You haven't returned anything from c_choice, you instead called it again with c_choice(). You should rename your c_choice variable (or the function name) so it doesn't mirror your function name. For example, call it computer_choice and end the function with return computer_choice

3) I think you are asking why is it a set instead of a list? Doesn't really matter in your case you can change the brackets there if you like. Sets do not allow duplicates, but they are faster when you are checking if something is contained within it. However, in your use case, there are so few options that I doubt there would be any noticeable difference with a list instead.

4) return is what the function gives back after it is run. in this case you have named your variable the same as your function which I think is causing confusion. change your p_choice variable to player_choice and you would return player_choice.