Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'operator': 0.03; 'elif': 0.05; 'string.': 0.05; 'variables': 0.07; '__name__': 0.09; 'filename': 0.09; 'function,': 0.09; 'latter': 0.09; 'namespace': 0.09; 'prefix': 0.09; 'separately': 0.09; 'try:': 0.09; 'val': 0.09; 'valueerror:': 0.09; 'subject:Help': 0.11; 'def': 0.12; 'suggest': 0.14; 'mostly': 0.14; 'random': 0.14; "'\\n')": 0.16; "'__main__':": 0.16; 'assignment?': 0.16; 'cleaner': 0.16; 'for,': 0.16; 'formatted': 0.16; 'prefer.': 0.16; 'scope.': 0.16; 'script,': 0.16; 'true:': 0.16; 'variations': 0.16; 'with?': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'input': 0.22; 'import': 0.22; 'this?': 0.23; '"you': 0.24; 'string,': 0.24; 'looks': 0.24; 'question': 0.24; 'sort': 0.25; "i've": 0.25; '15,': 0.26; 'class.': 0.26; 'this:': 0.26; 'header :In-Reply-To:1': 0.27; 'am,': 0.29; 'generally': 0.29; 'moved': 0.30; 'statement': 0.30; 'subject:please': 0.30; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; 'code': 0.31; 'comments': 0.31; 'easier': 0.31; 'class': 0.32; 'probably': 0.32; 'another': 0.32; 'style': 0.33; 'classes': 0.35; 'except': 0.35; 'test': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'done,': 0.36; 'false': 0.36; 'should': 0.36; 'clear': 0.37; 'to:addr:python-list': 0.38; 'track': 0.38; 'previous': 0.38; 'launch': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'how': 0.40; 'easy': 0.60; 'most': 0.60; "you're": 0.61; 'name': 0.63; 'such': 0.63; 'skip:n 10': 0.64; 'school': 0.64; 'kept': 0.65; 'mar': 0.68; 'results': 0.69; 'skip:r 30': 0.69; 'press': 0.70; 'wish': 0.70; 'below.': 0.71; 'score': 0.74; '2015': 0.84; 'homework': 0.84; 'maths': 0.84; 'scores': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=xS0MXvxRgdvxvqKbh5c5VZVdSya6tpehawaBZteUPF4=; b=rMMC065vwbHQWVzEXvcCCxcpCrlFuaoIOL0fgK8UTGQ6GtD9tyKD0OkzdG//p8/KNZ ucs0SNCUJbVqdW/1WZ3fNTSAJGqcx2tzfz1z4AztcLTdwJfPhXXCUwXMLQoxP00OtyJV ljgt1hHSi2/qDM4IT7cTU6+/12UpCVjQbVb/83ofDCgNJLHpViTD+g66X5zi25eELTwT lJVlYhS1T/YB04dfWb1QrU/JtLizmDjyuf9FAF4KZ4M1pSzzUrYtorhA8p/m2Kub6Ykp rBOBRPUwYbCB7GD+uqtdu7XlsE0z5jKeCUo2hiulnITgFppYu9ocGFHz9UcXIsrWOcX3 OYiA== X-Received: by 10.66.97.106 with SMTP id dz10mr128603343pab.66.1426435873779; Sun, 15 Mar 2015 09:11:13 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <43223410-538e-4834-82d4-98ab0702ae2d@googlegroups.com> References: <43223410-538e-4834-82d4-98ab0702ae2d@googlegroups.com> From: Ian Kelly Date: Sun, 15 Mar 2015 10:10:33 -0600 Subject: Re: Help me please urgently! To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 101 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1426435882 news.xs4all.nl 2925 [2001:888:2000:d::a6]:50364 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:87477 On Sun, Mar 15, 2015 at 9:43 AM, Jenny Hale wrote: > Hi > > How would I do this? > The teacher wants to keep track of the scores each member of the class obtains in the quiz. There are three classes in the school and the data should be kept separately for each class. Is this a homework assignment? It looks like you already have a solution or most of one. Do you have any specific question that you need help with? Since it's not clear to me what sort of help you're looking for, I've added some constructive style comments below. It mostly looks good to me. > import random > import operator > > MATHS_OPERATIONS = [ > (operator.add, "+"), > (operator.mul, "*"), > (operator.sub, "-") > ] > > NUM_QUESTIONS = 10 > > def get_int_input(prompt=''): > while True: > try: > return int(input(prompt)) > except ValueError: > print("Not a valid input (integer is expected)") > > def get_bool_input(prompt=''): > while True: > val = input(prompt).lower() > if val == 'yes': > return True > elif val == 'no': > return False It's generally polite to your user if you accept additional variations such as y/n. An easy way to do that is with the test ('yes'.startswith(val)) which will match the word 'yes' or any prefix of it. > else: > print("Not a valid input (yes/no is expected)") > > if __name__ == '__main__': This should all be moved into a function, and the body of the if statement would then just be a call to the function. This way you won't be polluting your global namespace with lots of variables that should really be local in scope. It also makes code reuse easier -- if another module wants to launch this script, all it has to do is import this module and call the function. > name = input("What is your name?").title() > class_name = input("What is your Class? ") > print(name, ", Welcome to the Maths Test") > > score = 0 > for _ in range(NUM_QUESTIONS): > num1 = random.randint(1,100) > num2 = random.randint(1,100) > op, symbol = random.choice(MATHS_OPERATIONS) > print("What is", num1, symbol, num2) > if get_int_input() == op(num1, num2): > print("Correct") > score += 1 > else: > print("Incorrect") > > print("Well done", name, "you scored", score, "/", NB_QUESTIONS) This would probably be a bit cleaner with a formatted string, either the % style or using str.format. The former would look like this: print("Well done, %s! You scored %d / %d." % (name, score, NB_QUESTIONS)) The latter is the new(er) hotness and would look like this: print("Well done, {}! You scored {} / {}.".format(name, score, NB_QUESTIONS)) It's up to you (and your instructor) which style you prefer. > > filename = class_name + ".txt" > > with open(filename, 'a') as f: > f.write(str(name) + " : " + str(score) + '\n') Another place where I would suggest a formatted string. > if get_bool_input("Do you wish to view previous results for your class"): > with open(filename, 'r') as f: > print(f.read()) > else: > input ("Press any key to exit") input is going to wait for the user to press Enter, not "any key".