Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Volleyball game
#1
I need to have user input scores for 5 volleyball games. The winner must score 25 points and win by 2. If the scores don't meet theses conditions, they need to be entered again. I keep trying different ways but can't get it completely correct.

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 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):
scorehome,scorevisitor=match(Home, Visitor,i+1)
if (scorehome > scorevisitor and scorehome < 25):
print("One team must score at least 25 points")
elif (scorehome > scorevisitor and scorehome - scorevisitor < 2):
print("One team must win by 2")
elif (scorevisitor > scorehome and scorevisitor < 25):
print("One team must score at least 25 points")
elif (scorevisitor > scorehome and scorevisitor - scorehome < 2):
print("One team must win by 2")
elif scorehome > scorevisitor:
winhome = winhome + 1
else:
scorevisitor > scorehome
winvisitor = winvisitor + 1
printScoreCard(Home,Visitor,winhome,winvisitor)

if winhome>winvisitor:
print(Home+" wins the game!")
else:
print(Visitor+" wins the game!")

startgame()
Reply
#2
Please edit your post so the code is in Python code tags. You can find help here. Also, when you copy code, use ctrl+shift+v to preserve indentation.
Reply
#3
I need to have user input scores for 5 volleyball games. The winner must score 25 points and win by 2. If the scores don't meet theses conditions, they need to be entered again. I keep trying different ways but can't get it completely correct.
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 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):
            scorehome,scorevisitor=match(Home, Visitor,i+1)
            if (scorehome > scorevisitor and scorehome < 25):
                  print("One team must score at least 25 points")
            elif (scorehome > scorevisitor and scorehome - scorevisitor < 2):
                  print("One team must win by 2")
            elif (scorevisitor > scorehome and scorevisitor < 25):
                  print("One team must score at least 25 points")
            elif (scorevisitor > scorehome and scorevisitor - scorehome < 2):
                  print("One team must win by 2")
            elif scorehome > scorevisitor:
                  winhome = winhome + 1
            else:
                  scorevisitor > scorehome
                  winvisitor = winvisitor + 1                                              
            printScoreCard(Home,Visitor,winhome,winvisitor)
            
      if winhome>winvisitor:
            print(Home+" wins the game!")
      else:
            print(Visitor+" wins the game!")      
     
startgame()
Reply
#4
In the for loop you are checking for conditions (team scoring at least 25 points, win by at least 2...). But if the condition is not met, program only prints out the warning message and goes on to next iteration (next match). Instead it should require the user to enter scores again, until they are proper.

Regarding design of program itself, I would recommend you to put checking for the conditions regarding scores in a new function. And call that function from within match function. Also, code readibility and maintainability would benefit greatly from following the famous PEP 8 style guide. In your case particularly 2 blank lines between function definitions. PEP 8 is a recommended read. Oh and in your else statement, scorevisitor > scorehome seems to have no use.
Reply
#5
The for loop is the part i can't figure out. I have tried many ways to get the user input repeated. I have tried to ask user again for scores but it still returns the result of the wrong scores. As far as making a new function and calling it in the match function, I am not sure how to do it. Do I still keep the conditions in the for loop or only in the new function? Does the new function need new parameters or are they the same as the ones in match function?
Reply
#6
While loop will be useful with getting input until the scores are right, something like
for i in range(5):
    while True:
        scorehome,scorevisitor=match(Home, Visitor,i+1)
        if check_score():
            break   
    printScoreCard(Home,Visitor,winhome,winvisitor)
 

I would put the conditions in the new function:

def check_score():
    if "score not right":
        "print warning message"
        return False
    "... same for other bad scores"
    else: 
        scorevisitor > scorehome
        winvisitor= winvisitor+1
        return True
Do you understand what I mean and how the code works?
Reply
#7
Thanks so much. I tried using a for loop with a while loop and could not get it work quite right. I think using the function is what I needed. I will put it together later on today and run it. Thanks again.
Reply
#8
(Dec-13-2017, 05:09 PM)DrewD Wrote: Thanks so much. I tried using a for loop with a while loop and could not get it work quite right. I think using the function is what I needed. I will put it together later on today and run it. Thanks again.

Sure thing. Keep in mind that what I suggested is just one way to implement solution, and not necessarily best one. I'm looking forward to see what you design :)
Reply
#9
I am still struggling. I can't figure out how to put it all together.

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)+": "))
      wrong_score()
 

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:
            scorevisitor > scorehome
            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():
                        break
            printScoreCard(Home,Visitor,winhome,winvisitor)
            
             
      if winhome>winvisitor:
            print(Home+" wins the game!")
      else:
            print(Visitor+" wins the game!")      
startgame()
Reply
#10
I get a none type object is not utterable error. I am not sure why.

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)+": "))
      

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:
            scorevisitor > scorehome
            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():
                        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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020