![]() |
|
Volleyball game - 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: Volleyball game (/thread-6860.html) Pages:
1
2
|
RE: Volleyball game - j.crater - Dec-16-2017 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. RE: Volleyball game - DrewD - Dec-17-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()
RE: Volleyball game - Terafy - Dec-17-2017 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. RE: Volleyball game - Terafy - Dec-17-2017 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 TrueThere 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
RE: Volleyball game - DrewD - Dec-17-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()
RE: Volleyball game - Terafy - Dec-17-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
RE: Volleyball game - DrewD - Dec-17-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()
RE: Volleyball game - Terafy - Dec-17-2017 Congrats! I knew it would of work hahaha!... sorry trying to be funny there. |