Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #87477

Re: Help me please urgently!

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 <ian.g.kelly@gmail.com>
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 <ian.g.kelly@gmail.com>
Date Sun, 15 Mar 2015 10:10:33 -0600
Subject Re: Help me please urgently!
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.395.1426435882.21433.python-list@python.org> (permalink)
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

Show key headers only | View raw


On Sun, Mar 15, 2015 at 9:43 AM, Jenny Hale <jhale7021@gmail.com> 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".

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Help me please urgently! Jenny Hale <jhale7021@gmail.com> - 2015-03-15 08:43 -0700
  Re: Help me please urgently! Chris Angelico <rosuav@gmail.com> - 2015-03-16 03:00 +1100
  Re: Help me please urgently! Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-15 10:10 -0600
  Re: Help me please urgently! Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-03-15 13:44 -0400

csiph-web