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