Python Forum
NEED HELP Pseudo-Random Numbers - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: NEED HELP Pseudo-Random Numbers (/thread-5759.html)



NEED HELP Pseudo-Random Numbers - Kongurinn - Oct-20-2017

Hey everyone. I've been coding for about a month now. Trying to figure out the assignment I got this week. THe assignment asked me to find if there were any shutouts in a simulation of games and report them. I defined shutout() and Summary(). I feel I am headed in the right direction. I just need a few more pointers.

from random import random

def main():
    printIntro()
    probA, probB, n = getInputs()
    winsA, winsB = simNGames (n, probA, probB)
    printSummary(winsA, winsB)
    Summary(shutoutsA, shutoutsB)

def printIntro():
    print("THis program simulates a game of racquetball between two")
    print('players called "A" and "B". The ability of each player is')
    print("the player wins the point when serving. Player A alwyas")
    print("has the first serve.")

def getInputs():
    #Returns the three simulation parameters
    a = float(input("What is the prob. player A wins a serve? "))
    b = float(input("What is the prob. player B wins a serve? "))
    n = int(input("How many games to simulate? "))
    return a, b, n

def simNGames(n, probA, probB):
    #Simulates n games of racquetball between players whose
    #   abilities are represented by the probability of winning a serve.
    #Returns number of wins for A and B
    winsA = winsB = 0
    for i in range(n):
        scoreA, scoreB = simOneGame(probA, probB)
        if scoreA > scoreB:
            winsA = winsA + 1
        else:
            winsB = winsB + 1
    return winsA, winsB

def simOneGame(probA, probB):
    #Simulates a single game of racquetball between players whose
    # abilities are represented by theprobability of winning a serve.
    #Returns final scores for A and B
    serving = "A"
    scoreA = 0
    scoreB = 0
    shutoutsA = 0
    shutoutsB = 0
    while not gameOver(scoreA,scoreB):
        if serving == "A":
            if random() < probA:
                scoreA = scoreA + 1
            else:
                serving = "B"
                
        else:
            if random() < probB:
                scoreB = scoreB + 1
            else:
                serving = "A"
        
    return scoreA, scoreB

def shutout(scoreA, scoreB):
    shutoutsA = 0
    shutoutsB = 0
    while simNGames(probA, probB):
        if scoreA == 0:
            shutoutsB = shutoutsB + 1
        if scoreB == 0:
            shutoutsA = shutoutsA + 1
    return shutoutsA, shutoutsB
    

def gameOver(a, b):
    #a and b represent scores for a racquetball game
    #Returns True if the game is over, False otherwise.
    return a==15 or b==15

def printSummary (winsA, winsB,):
    #Prints a summary of wins for each player.
    n = winsA + winsB
    print("\nGames simulated:", n)
    print("Wins for A: {0} ({1:0.1%})".format(winsA, winsA/n))
    print("Wins for B: {0} ({1:0.1%})".format(winsB, winsB/n))
    
def Summary (shutoutsA, shutoutsB):
    print(shutoutsA)
    print(shutoutsB)


if __name__== '__main__': main()



RE: NEED HELP Pseudo-Random Numbers - ichabod801 - Oct-20-2017

What problems are you having?


RE: NEED HELP Pseudo-Random Numbers - Kongurinn - Oct-20-2017

It either wont print at all or says 'shutoutsA' or 'shutoutsB' is not defined. It all depends where I go to try and print the 'shutouts'.

This is what I get:
Error:
"Traceback (most recent call last): File "C:/Users/kongurinn/Downloads/CS 1400/Exercise #9/rqtballPractice.py", line 87, in <module> if __name__== '__main__': main() File "C:/Users/Kongurinn/Downloads/CS 1400/Exercise #9/rqtballPractice.py", line 8, in main Summary(shutoutsA, shutoutsB) NameError: name 'shutoutsA' is not defined"



RE: NEED HELP Pseudo-Random Numbers - ichabod801 - Oct-20-2017

That's because you haven't defined them in the right scope. Variables defined in one function are not visible in other functions, at least not in Python. You have to explicitly pass them using parameters and return statements. The variables shutoutsA and shutoutsB seem to only get defined in simOneGame. But simOneGame does not return them, so not other function has access to those definitions.


RE: NEED HELP Pseudo-Random Numbers - Kongurinn - Oct-20-2017

I thought I passed the variables on when I coded these lines:

def shutout(scoreA, scoreB):
    shutoutsA = 0
    shutoutsB = 0
    while simNGames(probA, probB):
        if scoreA == 0:
            shutoutsB = shutoutsB + 1
        if scoreB == 0:
            shutoutsA = shutoutsA + 1
    return shutoutsA, shutoutsB
Shouldn't I be able to use the shutoutsA and shutoutsB variables?
How would you do it?


RE: NEED HELP Pseudo-Random Numbers - ichabod801 - Oct-20-2017

Yes, that would work, but I don't see where you call the shutout function in your code.


RE: NEED HELP Pseudo-Random Numbers - Skaperen - Oct-20-2017

when you do a return from a function, it returns the value, not the names under which that function stored the values.  when you return 2 values separated by a comma, you are returning a value which is a type known as a tuple, containing those 2 values.  you do need to call the function and do it in a way that properly uses the value the function will return, such as assigning that value to a variable, or to multiple variables.  you can also call a function and ignore the value it returns.

if you wish to have variables returned from a function, and have the code that called that function get the same names, it is possible to do.  but it is more complicated, requiring changes to how the function returns them and how the caller accepts them.  but, i suggest not ever thinking about something like that unless you have a specific reason to do it.  in my few years of programming in python, i have never encountered such a need.  i doubt if anyone here ever has, so i'm not going to explain how.


RE: NEED HELP Pseudo-Random Numbers - Kongurinn - Oct-22-2017

Can someone give me a short example of how to do this properly?

That's, honestly, how I learn best.


RE: NEED HELP Pseudo-Random Numbers - Larz60+ - Oct-22-2017

def function1(fielda, fieldb):
    print('fielda: {}, fieldb: {}'.format(fielda, fieldb)

def function2():
    field1 = 5
    field2 = 3
    function1(field1, field2)

if __name__ == '__main__':
    function2()
result:
Output:
fielda: 5, fieldb: 3
The field names don't have to be the same between functions (but they can be as well)


RE: NEED HELP Pseudo-Random Numbers - Skaperen - Oct-23-2017

i agree.  examples of minimal code, doing what is to be learned, and as little else as possible, while still making a reasonable example, is a good way to learn, for many people, including myself.  one other thing that usually helps is that the example be complete in the usability sense.  if it is a function or class or being given in that form, it should still be complete.  it should be possible to copy the code the a file, maybe named as specified and used in that form.  if it is a command, then it should be able to run as a command.  example code that genuinely needs to import a module that does not come built-in to python should be indicated with how a regular user can install it.  it is best to avoid the need to install anything for an example unless the example is illustrating how to use what is to be installed.

i love to get examples.