0

SO I'm making a program about hockey players playing and I will record their goals.

Here's what should happen:

Who scored? 4
Who scored? 5
Who scored? 6
Who scored? 4
Who scored? 3
Game still going?(y/n) y
Who scored? 3
Who scored? 2
Who scored? 5
Who scored? 2
Who scored? 3
Game still going?(y/n) n

Creating a histogram from values: 
Element Goals Histogram
      1     0 
      2     2 **
      3     2 ***
      4     2 **
      5     2 **
      6     1 *
      7     1 *
      8     0 
      9     0 
     10     0 

Here is my code:

def info():
    ranking = [0,0,0,0,0,0,0,0,0,0,0]
    survey = []
    return ranking,survey
def info2(survey):   
    x = ''
    for i in range(0,5):
        x = int(input("Who scored?"))           
        survey.append(x)
    again(x)
    return survey
def info3(ranking,survey):
    for i in range(len(survey)):
        ranking[survey[i]]+=1
    return ranking, survey

def again(x):
    y = input("Game still on? y/n").lower()
    if y == "yes" or y == "y":
        info()
    elif y == "n" or y =="no":
        hg(x)


#create histogram
def hg():
    print("\nCreating a histogram from values: ")
    print("%3s %5s %7s"%("Element", "Goals", "Histogram"))

#start from element 1 instead of 0
    for i in range(len(ranking)-1):
        print("%7d %5d %-s"%(i+1, ranking[i+1], "*" * ranking[i+1]))

def main():
    x,y = info()
    a = info2(y)
    d = again(x)
    b,c = info3(x,a)
    z = hg(x)
main()

When I run this as it is, I get the Who scored thing, and I enter 'y' on the y/n and it works, but when I enter y/n and i put n, it prints the "element goals histogram" then throws the following:

Traceback (most recent call last):
 line 48, in <module>
    main()
 line 44, in main
    a = info2(y)
 line 17, in info2
    again(x)
 line 29, in again
    hg(x)
 line 39, in hg
    for i in range(len(ranking)-1):
NameError: name 'ranking' is not defined
Registered User
  • 468
  • 1
  • 7
  • 31

3 Answers3

2
x = input("Game still on? y/n").lower

instead should be:

x = input("Game still on? y/n").lower()
mchant
  • 320
  • 1
  • 8
1

There are a couple issues that I see with the code...

First, you've got lower instead of lower() in your again() function. This binds the function itself to x instead of calling it and assigning its return value.

Also, your hg() function expects an argument, but you don't pass one in here. The ranking defined in info() is local to that function, and not visible from hg().

Edit in response to OP after OP's code was updated based on my comments above:

Also, there are issues with your handling of the exit case in again(). I think you shouldn't call hg() at all there, and instead return the answer to a separate variable in info2().

So that code for those two functions would then look something like this:

def info2(survey):
    x = ''
    ans = 'y'

    while ans in ('y', 'yes'):
        for i in range(0,5):
            x = int(input("Who scored?"))
            survey.append(x)
        ans = again()
    return survey

def again():
    x = input("Game still on? y/n").lower()
    if x == "yes" or x == "y":
        info()

    return x

Note the use of the additional variable, and the pass.

Edit in response to 2nd comment from OP:

info3() is unchanged. I added again() and info2() with my changes. You would keep info3() as is (at least as regards this particular question).

Also, since my change just had pass in the No case, it can actually be removed entirely. Just check for the Yes case, and otherwise return (an else isn't even required in this particular case).

When I run the code with the changes I mentioned, it appears to work as required. This is example output:

Who scored?1
Who scored?1
Who scored?1
Who scored?2
Who scored?2
Game still on? y/ny
Who scored?3
Who scored?3
Who scored?3
Who scored?2
Who scored?2
Game still on? y/nn

Creating a histogram from values: 
Element Goals Histogram
      1     3 ***
      2     4 ****
      3     3 ***
      4     0 
      5     0 
      6     0 
      7     0 
      8     0 
      9     0 
     10     0 
khampson
  • 14,700
  • 4
  • 41
  • 43
0

I'm not sure what all of the functions and variables were supposed to do. It's easier to debug and understand code if you use sensible names for functions and variables.

from collections import OrderedDict

def main():
    list_of_players = [str(number) for number in range(1, 11)]
    ranking = play(list_of_players)
    print_histogram(ranking)

def play(list_of_players):
    ranking = OrderedDict([(player, 0) for player in list_of_players])
    while True:
        for i in range(5):
            player = input('Who scored? ')
            try:
                ranking[player] += 1
            except KeyError:
                print('Not a player')
        if input('Game still going?(y/n) ').lower() in ['n', 'no']:
            return ranking

def print_histogram(ranking):
    template = '{player:^7} {goals:^7} {stars}'
    print('\nCreating a histogram from values: ')
    print(template.format(player='Element', goals='Goals', stars='Histogram'))
    for player, goals in ranking.items():
        print(template.format(player=player, goals=goals, stars='*' * goals))

if __name__ == '__main__':
    main()
Håken Lid
  • 22,318
  • 9
  • 52
  • 67