Python mark book?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Florrianer
    New Member
    • May 2012
    • 3

    Python mark book?

    Code:
    def markbook():
    
        names = []      
        marks = []      
    
        while True:     
            name = raw_input("Name: ")      
          
            if name == "exit":      
                break
            elif name == "print":     
                print "Name" + "      " + "Mark"
                for x in range(len(names)):
                    print names[x] + "     " + marks[x]
            else:
                names.append(name)      
                mark = raw_input("Mark: ")      
                marks.append(mark)
    How would have written this program if you had to use dictionaries instead of lists?
    Is it possible to write this markbook using tuples?
    Last edited by bvdet; May 17 '12, 06:50 PM. Reason: Ad code tags
  • andrean
    New Member
    • May 2012
    • 5

    #2
    Code:
    def markbook():
        names = dict()
    
        while True: 
            name = raw_input("Name: ") 
    
            if name == "exit": 
                break
            elif name == "print": 
                print "Name" + " " + "Mark"
                for name, mark in names.items():
                    print "{0} {1}".format(name, mark)
            else:
                mark = raw_input("Mark: ") 
                names[name] = mark 
    
    def markbook_tuple():
        names = list()
    
        while True: 
            name = raw_input("Name: ") 
    
            if name == "exit": 
                break
            elif name == "print": 
                print "Name" + " " + "Mark"
                for name, mark in names:
                    print "{0} {1}".format(name, mark)
            else:
                mark = raw_input("Mark: ") 
                names.append((name, mark))

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Tuples would not work as coded because they are immutable. A dictionary would work. The names could be the dictionary keys and marks could be stored in a list for each unique key. Maybe like this:
      Code:
      def markbook():
       
          dd = {}
       
          while True:     
              name = raw_input("Name: ")      
       
              if name == "exit":      
                  return dd
              elif name == "print":     
                  print "Name" + "      " + "Mark"
                  names = dd.keys()
                  for name in names:
                      for item in dd[name]:
                          print name + "     " + item
              else:
                  dd.setdefault(name, [])    
                  mark = raw_input("Mark: ")      
                  dd[name].append(mark)
      
      if __name__ == "__main__":
          print markbook()
      Last edited by bvdet; May 17 '12, 07:18 PM. Reason: edit elif name == "print":

      Comment

      Working...