473,412 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Python mark book?

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))

2 2117
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
2,851 Expert Mod 2GB
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

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

Similar topics

23
by: Anthony | last post by:
Hello I'm currently trying to read Core Python Programming, but by the looks of it im never going to get done the book is about 860 pages long real intimidating, but im guess im going to have to...
3
by: scn | last post by:
does anyone know when the next edition of 'learning python' is due for release? thank you. scott
14
by: Eduardo Patto Kanegae | last post by:
Hello, I have been programming with Visual Basic and PHP in the last 5 years and some folks had recommended Python a free language.... I had looked for a Python book to start up but found many...
5
by: Logan | last post by:
Is there something like a roadmap for Python3 (i.e. v3.0)? IIRC, formerly people talked about Python 3000 resp. P3k and it was speculated, that Python3 will not be backwards compatible with...
0
by: David Mitchell | last post by:
Hello group, I'm trying to create a TCP server using Python, and I want it to run under Windows as a service. Now, I'm fine with building the TCP server using Python - done it lots of times,...
18
by: Michael McGarry | last post by:
Hi, What is the best book covering Python? Michael
9
by: santanu | last post by:
Hi all, I know a little python (not the OOP part) learnt by studying the online tutorial. Now I would like to learn it more thoroughly. I have access to 'Programming Python' which I liked...
10
by: Markus Franz | last post by:
Hi! I wanted to run some Python code from inside a C program, so I did it like it was explained in the Python manual: #include <Python.h> int main(int argc, char *argv) { Py_Initialize();...
2
by: progman | last post by:
new to python. i work with db heavily. any good book for python + database?????
3
by: nik | last post by:
I have a VB6 application that I would like to attach to a python communications application. I have come across several discussions on using VB with a Python COM back-end, but no starting point....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.