Help with a class project (Functions and %)

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 904 Views - Last Post: 27 April 2013 - 02:14 PM Rate Topic: -----

#1 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Help with a class project (Functions and %)

Posted 19 April 2013 - 07:45 AM

I am taking the coursera class: An Introduction to Interactive Programming in Python by Joe Warren, John Greiner, Stephen Wong, Scott Rixner. I am loving this class so far but i am having trouble with the week 1 mini - project (rock, paper, scissors, lizard, Spock). I had to define two variables (number_to_name, and name_to_number), and after a many attempts, i figured that out. The instructors gave me code to paste my product in to test and everything seemed to work out. The last part i had to work on is to define the function rpsls. The instructions for this part is as follows:

1. Build the first part of the main function rpsls(name) that converts name into the number player_number between 0 and 4 using the helper function name_to_number.
2. Build the second part of rpsls(name) that generates a random number comp_number between 0 and 4 using the function random.randrange(). I suggest experimenting with randrange in a separate CodeSkulptor window before deciding on how to call it to make sure that you do not accidently generate numbers in the wrong range.
2. Build the last part of rpsls(name) that determines and prints out the winner. This test is actually very simple if you apply modular arithmetic (% in Python) to the difference between comp_number and player_number. If this is not immediately obvious to you, I would suggest reviewing our discussion of remainder and modular arithmetic and experimenting with the remainder operator % in a separate CodeSkulptor window to understand its behavior.

This is the code i have created so far:

def number_to_name(number):
    
    if number == 0:
        return "rock"
#I dont want to paste all the code due to other students but i hope you get the idea.

def name_to_number(name):
    # fill in your code below
    if name == "rock":
        return 0

#What i needed to do is implement step one into the following function...

def rpsls(name): 
    # fill in your code below
    

    # convert name to player_number using name_to_number

    # compute random guess for comp_number using random.randrange()

    # compute difference of player_number and comp_number modulo five

    # use if/elif/else to determine winner

    # convert comp_number to name using number_to_name
    
    # print results




So can someone help me get started with the function rpsls, or the least help me get started in the right direction. I would greatly appreciate it. I just have no idea how to start or to implement modulo. Thanks for the help!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with a class project (Functions and %)

#2 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: Help with a class project (Functions and %)

Posted 19 April 2013 - 08:34 AM

Well, before trying to get into the body of any of the game code, I would like to talk about your first two functions. Yes, what you have done will work (IE enumerating all the options with a long if/elif/else block); but as a general rule of thumb, if you find yourself doing this in Python, you are doing something wrong.

Examine the following:
#Global constant (put it in the functions if your teacher complains)
OPTIONS = ["rock","paper","scissors","lizard","Spock"]

def number_to_name(number):
    return OPTIONS[number]

def name_to_number(name):
    return OPTIONS.index(name)

This is a much more pythonic approach (and is much, much shorter).

-Mek
Was This Post Helpful? 1
  • +
  • -

#3 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 19 April 2013 - 09:00 AM

Thanks Mek,

But I have to leave that the same. There is a grading rubric and by that rubric I can't use something not yet taught in the course. I guess they want us to use and learn the if else statements before using something new. My question is that in the third function how do I start, going by the functions?
Was This Post Helpful? 0
  • +
  • -

#4 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: Help with a class project (Functions and %)

Posted 19 April 2013 - 09:22 AM

Well, your first call should be:
# convert name to player_number using name_to_number
player_number = name_to_number(name)
I not really sure what your question is. Follow the steps and post your attempt and we can work further.

-Mek
Was This Post Helpful? 1
  • +
  • -

#5 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 19 April 2013 - 10:40 AM

Thank you! I just couldn't understand what it wanted me to do. I will post back my later code if I have issues.
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Help with a class project (Functions and %)

Posted 19 April 2013 - 11:33 AM

Just took a quick spin by the class. ( I'm in the middle of the Scala class, so familiar with site. )

In the tips, you'll want to look at random_dice.

Also, don't limit yourself to those two functions. Write more if you like.

For those playing along at home, the full template offered is:
Spoiler


The expected output:
Spoiler

Was This Post Helpful? 1
  • +
  • -

#7 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 25 April 2013 - 06:13 AM

View PostMekire, on 19 April 2013 - 09:22 AM, said:

Well, your first call should be:
# convert name to player_number using name_to_number
player_number = name_to_number(name)
I not really sure what your question is. Follow the steps and post your attempt and we can work further.

-Mek


Sorry i havent replied sooner, Mek, Work has been crazy this week. Ok so this is what i got so far...

def rpsls(name): 
    # fill in your code below
    player_number = name_to_number(name)
    comp_number = random.randrange(0,4)
    determine = (player_number - comp_number) % 5
    if determine ==



So how to i get the determine. How do i know wat to compare it to?
Was This Post Helpful? 0
  • +
  • -

#8 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: Help with a class project (Functions and %)

Posted 25 April 2013 - 06:47 AM

Assuming the way you assigned the numbers corresponds to the numbers in baavgai's post,
(player_number - comp_number) % 5
should always have certain values when you lose and certain values when you win (and be equal to zero when you draw). Try to figure out what those values are.

-Mek
Was This Post Helpful? 1
  • +
  • -

#9 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 25 April 2013 - 10:50 AM

Ok, Mek, see I thought that there were two or tgree different values if you win. Thanks for the help. Now for some pen and paper.
Was This Post Helpful? 0
  • +
  • -

#10 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 05:29 AM

Ok so what I found is that if the player loses, the number is negative. If the player wins then the number is positive. A tie is zero. How do I make it react to all negative or positive numbers.
Was This Post Helpful? 0
  • +
  • -

#11 Mekire  Icon User is offline

  • D.I.C Head

Reputation: 111
  • View blog
  • Posts: 204
  • Joined: 11-January 13

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 07:24 AM

This:
(player_number - comp_number) % 5
should never be negative. What exactly are you doing?

-Mek
Was This Post Helpful? 1
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 08:03 AM

Indeed.

You have a computer, you can easily test this yourself.

e.g.
>>> for x in range(5):
...     for y in range(5):
...             print "(",x, "-", y, ")%5 =", ((x-y)%5)
... 
( 0 - 0 )%5 = 0
( 0 - 1 )%5 = 4
( 0 - 2 )%5 = 3
( 0 - 3 )%5 = 2
( 0 - 4 )%5 = 1
( 1 - 0 )%5 = 1
( 1 - 1 )%5 = 0
( 1 - 2 )%5 = 4
( 1 - 3 )%5 = 3
( 1 - 4 )%5 = 2
( 2 - 0 )%5 = 2
( 2 - 1 )%5 = 1
( 2 - 2 )%5 = 0
( 2 - 3 )%5 = 4
( 2 - 4 )%5 = 3
( 3 - 0 )%5 = 3
( 3 - 1 )%5 = 2
( 3 - 2 )%5 = 1
( 3 - 3 )%5 = 0
( 3 - 4 )%5 = 4
( 4 - 0 )%5 = 4
( 4 - 1 )%5 = 3
( 4 - 2 )%5 = 2
( 4 - 3 )%5 = 1
( 4 - 4 )%5 = 0
>>> 


Was This Post Helpful? 1
  • +
  • -

#13 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 10:48 AM

See this is what I mean. The answers range from one to four. I'm still confused.I worked it out similar to above just to realize what I already knew.
Was This Post Helpful? 0
  • +
  • -

#14 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 11:02 AM

Look at the information you have in front of you...

# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors

# compute difference of player_number and comp_number modulo five

This would be the we just saw above above.

# use if/elif/else to determine winner

Ok... you know that the result of your operation is 0..4. You also know, looking at the data, that 0 is a tie. You now have results 1..4 to consider. And you know that, if the answer isn't 0, there are two results that win and two results that loose. Further, you can find out what the expected results are, e.g Spock vaporizes rock.

Assume that the meaning of each result value is consistent. That is 1 is either always a loss or always a win. Look at the data you have in front of you. Can if you figure out a test that always gives a correct result? That is the answer to the problem.
Was This Post Helpful? 1
  • +
  • -

#15 tpaskell  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 125
  • Joined: 21-March 13

Re: Help with a class project (Functions and %)

Posted 26 April 2013 - 01:06 PM

Thank you very much baavgai and Mek! I figured it out and added the final print statements, and i will be turning it in later. Again thank you both for helping and clearing some things up for me! Much appreciated!

-Tristan
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2