[Tutor] Hi there!

Mats Wichmann mats at wichmann.us
Wed Dec 18 09:40:45 EST 2019


On 12/18/19 3:40 AM, Hamnah Baig wrote:
> Can you help me with this code?
> I'm struggling to create a leaderboard/score system and a login system
> along with an exit option that exits and goes back to the start.

just a couple of quick notes, I'm sure you'll get some others.

1. what's the problem?  something is causing you to ask for help - if 
there are errors, or unexpected behavior, please describe it.

2. your code runs on, and on, and on... it would be much more readable 
if split into functions that do distinct things. Have you by any chance 
not gotten to learning about functions yet?

2b. in particular, one chunk of code is repeated exactly, with just 
variable names changed (the rolling for each user), that should be a 
hint to you that you should refactor that to use a function.

3. you define some stuff you don't use... like the even and odd arrays. 
I see you've appropriately computed even/odd through a modulus operation 
and so you don't need these declarations. You have a lot of other things 
that look like you thought you needed then, then you commented them out. 
Now they're just clutter.

4. at the very end you try to print totals, but you have never used 
these variables:

> p1go = 0
> p2go = 0
...
> #Final Scores
> 
> print("\nThe scores have been added up and")
> time.sleep(2)
> print('\n')
> print(pname1 , "got", p1go, ".")
> print(pname2 , "got", p2go, ".")

you are collecting the totals as you go into differently named things, 
so you should use those...

BUT:

5. your instructions imply that you're looking for a high roll.

         print('\nRules')
         print('\nThe dice game is a very simple game to understand:')
         print('\nTwo players take turns to roll two dices.')
         print("\nThey both roll the dice twice over 5 rounds")
         print("\nthe way to win is who ever has the highest even number

you're not doing anything with this... you seem to add 10 if the roll is 
even (and subtract 5 if odd), but then instead you're summing the rolls, 
not recording a high roll.  Is this intended? Where do the +10 and -5 
come from?  If that's not obvious (it isn't to me), then there should 
have been a comment explaining what that was for.

6. you  suggest that options 1 to 4 are valid, but do nothing with a 
value of 4


   while int(opt) < 1 or int(opt) > 4:
     print('That is not a valid option.')
     print('Please enter a number between 1 and 4:')


A lot of the problems are the result of hacking: you started writing, 
and then you changed things, and then you changed things.  A better way 
to attack such a problem is in small chunks. You "decompose" the problem 
into small bits of functionalty, code just that piece, and test it, 
substituting hardcoded or otherwise "mocked" values where those would 
have needed stuff from the rest of the program you haven't written yet. 
Then build it up, making sure each new piece doesn't break the pieces 
you already wrote and tested.



More information about the Tutor mailing list