Posts: 1,150
Threads: 42
Joined: Sep 2016
You still have that scorevisitor > scorehome in wrong_score which serves no purpose :)
You get this error because function match doesn't return anything.
scorehome,scorevisitor=match(Home, Visitor,i+1)
match function needs to return values that will be assigned to scorehome and scorevisitor
And about your looping until user enters proper scores, look into continue instead of break.
Posts: 9
Threads: 1
Joined: Dec 2017
Still struggling...
def match(Home, Visitor, matchnum):
print("Match", str(matchnum))
scorehome = int(input("Enter the number of points "+Home+" got in Match "+str(matchnum)+": "))
scorevisitor = int(input("Enter the number of points "+Visitor+" got in Match "+str(matchnum)+": "))
return scorehome, scorevisitor
def printScoreCard(Home,Visitor,winhome,winvisitor):
print(Home,winhome)
print(Visitor, winvisitor)
def wrong_score():
if (scorehome > scorevisitor and scorehome < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorehome > scorevisitor and scorehome - scorevisitor < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor - scorehome < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
elif scorehome > scorevisitor:
winhome=winhome+1
return True
else:
winvisitor= winvisitor+1
return True
def startgame():
print("Welcome to the volleyball score program.")
Home = input("Enter the home team's name: ")
Visitor = input("Enter the visitor's team name: ")
winhome = 0
winvisitor = 0
for i in range(5):
while True:
scorehome,scorevisitor=match(Home, Visitor,i+1)
if wrong_score():
continue
elif scorehome > scorevisitor:
winhome=winhome+1
else:
winvisitor=winvisitor+1
printScoreCard(Home,Visitor,winhome,winvisitor)
if winhome>winvisitor:
print(Home+" wins the game!")
else:
print(Visitor+" wins the game!")
startgame()
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 04:48 AM
(This post was last modified: Dec-17-2017, 04:48 AM by Terafy.)
Found a problem.
The function "wrong_score()" had an error. You did not pass "scorehome" and "scorevisitor". Also winhome is not defined in the function.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 05:39 AM
(This post was last modified: Dec-17-2017, 05:39 AM by Terafy.)
Anyway, for the "wrong_score" function. I suggest you to:
1) pass the "scorehome" and "scorevisitor" into the function.
2) replace this part of your code.
elif scorehome > scorevisitor:
winhome=winhome+1
return True
else:
winvisitor= winvisitor+1
return True There is an error here, because "winhome" and "winvisitor" are not a local variable. Don't try to make the function calculate, just stick with checking the scores. So remove "winhome = winhome + 1" and "winvisitor= winvisitor+1" and tidy up the code with an else-statement.
For the "startgame()" function, we went through that the "wrong_score()" should just return a True or False, so we don't need the continue. But you need change the elif and make it to another if-statement to add the scores when the when the "wrong_score()" returns True it would add the winning teams points.
if wrong_score():
continue
elif scorehome > scorevisitor:
winhome=winhome+1
else:
winvisitor=winvisitor+1
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 9
Threads: 1
Joined: Dec 2017
I am able to run the program, but it only records scores if the wrong scores are put in first and then corrected. If I put in correct scores initially it does not record them. Ugh!
def match(Home, Visitor, matchnum):
print("Match", str(matchnum))
scorehome = int(input("Enter the number of points "+Home+" got in Match "+str(matchnum)+": "))
scorevisitor = int(input("Enter the number of points "+Visitor+" got in Match "+str(matchnum)+": "))
return scorehome, scorevisitor
return wrong_score(scorehome, scorevisitor)
def printScoreCard(Home,Visitor,winhome,winvisitor):
print(Home,winhome)
print(Visitor, winvisitor)
def wrong_score(scorehome, scorevisitor):
if (scorehome > scorevisitor and scorehome < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorehome > scorevisitor and scorehome - scorevisitor < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor - scorehome < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
else:
return True
def startgame():
print("Welcome to the volleyball score program.")
Home = input("Enter the home team's name: ")
Visitor = input("Enter the visitor's team name: ")
winhome = 0
winvisitor = 0
for i in range(5):
while True:
scorehome,scorevisitor=match(Home, Visitor,i+1)
if wrong_score(scorehome, scorevisitor):
break
if scorehome > scorevisitor:
winhome=winhome+1
else:
winvisitor=winvisitor+1
printScoreCard(Home,Visitor,winhome,winvisitor)
if winhome>winvisitor:
print(Home+" wins the game!")
else:
print(Visitor+" wins the game!")
startgame()
Posts: 69
Threads: 0
Joined: Dec 2017
Well... this prove I gave you a bad explanation... I was going for
if wrong_score(scorehome, scorevisitor):
if scorehome > scorevisitor:
winhome=winhome+1
else:
winvisitor=winvisitor+1
break
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
Posts: 9
Threads: 1
Joined: Dec 2017
Finally....it works! Thank you for all your help!
def match(Home, Visitor, matchnum):
print("Match", str(matchnum))
scorehome = int(input("Enter the number of points "+Home+" got in Match "+str(matchnum)+": "))
scorevisitor = int(input("Enter the number of points "+Visitor+" got in Match "+str(matchnum)+": "))
return scorehome, scorevisitor
return wrong_score(scorehome, scorevisitor)
def printScoreCard(Home,Visitor,winhome,winvisitor):
print(Home,winhome)
print(Visitor, winvisitor)
def wrong_score(scorehome, scorevisitor):
if (scorehome > scorevisitor and scorehome < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor < 25):
print("One team must score 25 points. Please enter scores again.")
return False
elif (scorehome > scorevisitor and scorehome - scorevisitor < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
elif (scorevisitor > scorehome and scorevisitor - scorehome < 2):
print("One team must win by 2 points. Please enter scores again.")
return False
else:
return True
def startgame():
print("Welcome to the volleyball score program.")
Home = input("Enter the home team's name: ")
Visitor = input("Enter the visitor's team name: ")
winhome = 0
winvisitor = 0
for i in range(5):
while True:
scorehome,scorevisitor=match(Home, Visitor,i+1)
if wrong_score(scorehome, scorevisitor):
if scorehome > scorevisitor:
winhome=winhome+1
else:
winvisitor=winvisitor+1
break
printScoreCard(Home,Visitor,winhome,winvisitor)
if winhome>winvisitor:
print(Home+" wins the game!")
else:
print(Visitor+" wins the game!")
startgame()
Posts: 69
Threads: 0
Joined: Dec 2017
Dec-17-2017, 08:28 PM
(This post was last modified: Dec-17-2017, 08:28 PM by Terafy.)
Congrats! I knew it would of work hahaha!...
sorry trying to be funny there.
When my code doesn't work I don't know why **think** and when my code works I don't know why **think**
|