By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,747 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,747 IT Pros & Developers. It's quick & easy.

Python mark book?

P: 3
Expand|Select|Wrap|Line Numbers
  1. def markbook():
  2.  
  3.     names = []      
  4.     marks = []      
  5.  
  6.     while True:     
  7.         name = raw_input("Name: ")      
  8.  
  9.         if name == "exit":      
  10.             break
  11.         elif name == "print":     
  12.             print "Name" + "      " + "Mark"
  13.             for x in range(len(names)):
  14.                 print names[x] + "     " + marks[x]
  15.         else:
  16.             names.append(name)      
  17.             mark = raw_input("Mark: ")      
  18.             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?
May 17 '12 #1

✓ answered by andrean

Expand|Select|Wrap|Line Numbers
  1. def markbook():
  2.     names = dict()
  3.  
  4.     while True: 
  5.         name = raw_input("Name: ") 
  6.  
  7.         if name == "exit": 
  8.             break
  9.         elif name == "print": 
  10.             print "Name" + " " + "Mark"
  11.             for name, mark in names.items():
  12.                 print "{0} {1}".format(name, mark)
  13.         else:
  14.             mark = raw_input("Mark: ") 
  15.             names[name] = mark 
  16.  
  17. def markbook_tuple():
  18.     names = list()
  19.  
  20.     while True: 
  21.         name = raw_input("Name: ") 
  22.  
  23.         if name == "exit": 
  24.             break
  25.         elif name == "print": 
  26.             print "Name" + " " + "Mark"
  27.             for name, mark in names:
  28.                 print "{0} {1}".format(name, mark)
  29.         else:
  30.             mark = raw_input("Mark: ") 
  31.             names.append((name, mark))

Share this Question
Share on Google+
2 Replies

P: 5
Expand|Select|Wrap|Line Numbers
  1. def markbook():
  2.     names = dict()
  3.  
  4.     while True: 
  5.         name = raw_input("Name: ") 
  6.  
  7.         if name == "exit": 
  8.             break
  9.         elif name == "print": 
  10.             print "Name" + " " + "Mark"
  11.             for name, mark in names.items():
  12.                 print "{0} {1}".format(name, mark)
  13.         else:
  14.             mark = raw_input("Mark: ") 
  15.             names[name] = mark 
  16.  
  17. def markbook_tuple():
  18.     names = list()
  19.  
  20.     while True: 
  21.         name = raw_input("Name: ") 
  22.  
  23.         if name == "exit": 
  24.             break
  25.         elif name == "print": 
  26.             print "Name" + " " + "Mark"
  27.             for name, mark in names:
  28.                 print "{0} {1}".format(name, mark)
  29.         else:
  30.             mark = raw_input("Mark: ") 
  31.             names.append((name, mark))
May 17 '12 #2

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. def markbook():
  2.  
  3.     dd = {}
  4.  
  5.     while True:     
  6.         name = raw_input("Name: ")      
  7.  
  8.         if name == "exit":      
  9.             return dd
  10.         elif name == "print":     
  11.             print "Name" + "      " + "Mark"
  12.             names = dd.keys()
  13.             for name in names:
  14.                 for item in dd[name]:
  15.                     print name + "     " + item
  16.         else:
  17.             dd.setdefault(name, [])    
  18.             mark = raw_input("Mark: ")      
  19.             dd[name].append(mark)
  20.  
  21. if __name__ == "__main__":
  22.     print markbook()
May 17 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.