Bottom Page

Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My version GhostGame
#1
from random import randint

def get_msg(msg, subject, subject_mark):
    for i in range(1, doors+1):
        if i in subject:
            msg+=door % subject_mark
        else:
            msg+=door % ' '
    return msg

def get_input(msg, min_, max_):
    while True:
        s=input(msg)
        if s.isdigit() and min_ <= int(s) and int(s) <= max_:
            return int(s)
        else:
            print('Bad choice! Try again.')

def get_ghost():
    ghost = []
    while True:
        rnd = randint(1, doors)
        if rnd not in ghost:
            ghost.append(rnd)
        if len(ghost)==2:
            return ghost

print('GhostGame\n')
door='__[ %s ]__'
score, best_score, game = 0, 0, 1
doors = get_input('Enter the number of door[3..8] >> ', 3, 8)
show_doors=''.join([door % i for i in range(1, doors+1)])
while True:
    print(show_doors)
    choice = get_input('Choose your door from 1 to %s >> ' % doors, 1, doors)
    ghost = get_ghost()
    print(get_msg('Ghost is here:\n', ghost, 'X'))
    print(get_msg('You is here:\n', [choice], 'O'))
    if choice in ghost:
        if score > best_score:
            best_score=score
        print('\n\nGame over.\nGame: %s. Your score: %s.' % (game, score))
        s = input('Would you like to play again ? [Yes]/No >> ')
        if s.lower() in ['no', 'n']:
            break
        score=0
        game+=1
    else:
        score += 1
        print('\n\nGame: %s. Score: %s.' % (game, score))
print('Games: %s. Best score: %s.' % (game, best_score))
What do you think, about code?
Quote
#2
I think I'd rather write get_ghost() like this, so it's obvious that it always returns a list:
def get_ghost():
    ghost = []
    while len(ghost) < 2:
        rnd = randint(1, doors)
        if rnd not in ghost:
            ghost.append(rnd)
    return ghost
Quote
#3
And running it through pylint would help make it conform to pep8.
Quote
#4
nilamo thank you
Quote
#5
My thoughts: There are too many single letter variable names, try to be more descriptive. I don't like the _ after key words so they can be variable names. I would use other names, like low and high instead of min_ and max_. I would put the game loop in a function, and call it from an if __name__ == '__main__' block. Note that you can use random.sample to get two unique numbers without having to code the loop in get_ghost.

From the user perspective it works fine, but I would add a description of the game. I was rather clueless playing it the first time.

Don't take this as too critical. The only thing that really bothers me is the single letter variable names.

Oh, and comments would be good.
ljmetzger likes this post
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#6
(Jun-18-2018, 10:20 PM)ichabod801 Wrote: I don't like the _ after key words so they can be variable names. I would use other names, like low and high instead of min_ and max_.
I agree, me too. Thank you for low and high, I will use it.

(Jun-18-2018, 10:20 PM)ichabod801 Wrote: Note that you can use random.sample to get two unique numbers without having to code the loop in get_ghost.
Thank you, I did not know about this possibility.

(Jun-18-2018, 10:20 PM)ichabod801 Wrote: There are too many single letter variable names, try to be more descriptive.
I use when the variable appears and then disappears. I'm not sure that this is bad.

And again thanks nilamo for the version get_ghost(). I like it.
Quote
#7
(Jun-20-2018, 06:25 PM)anickone Wrote: I use when the variable appears and then disappears. I'm not sure that this is bad.

All variables appear and then disappear.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote
#8
(Jun-20-2018, 08:44 PM)ichabod801 Wrote: All variables appear and then disappear.
Of course, but I was talking about following several lines.

My code :)
import re, os

def make_log(msg):
    print(msg)
    log.append(msg)

def search_one_chr(py_file):
    with open(py_file) as fin:
        lines=fin.readlines()
    file_show=1
    for i, line in enumerate(lines,1):
        r=rex.findall(line)
        if r:
            if file_show:
                file_show=0
                make_log('%s\nfile = "%s"' % ('*'*50, py_file))
            c=r[0]
            if c in chrs:
                chrs[c]+=1
            else:
                chrs[c]=1
            make_log('line = %s, code = "%s"' % (i, line.rstrip()))

def log_save(log, log_file):
    with open(log_file, 'w') as fout:
        fout.write('\n'.join(log))

pattern=r"^(?:\t*| *)([a-zA-Z]) = "
rex=re.compile(pattern)
path='/usr/lib/python3.5/'
path_code_source=os.path.abspath(path)
chrs={}
log=[]
for root, dirs, files in os.walk(path_code_source):
    for next_file in files:
        if next_file.endswith('.py'):
            search_one_chr(os.path.join(root,next_file))
cnt=0
for c in sorted(chrs.keys()):
    cnt+=chrs[c]
    make_log('letter %s as var used %s times' % (c,chrs[c]))
make_log('all used one letter as var %s times' % cnt)
log_save(log, 'log_file.txt')
For python dir.
Output:
letter A as var used 4 times letter B as var used 2 times letter H as var used 2 times letter I as var used 2 times letter K as var used 1 times letter L as var used 22 times letter M as var used 8 times letter N as var used 1 times letter P as var used 2 times letter R as var used 2 times letter S as var used 1 times letter T as var used 11 times letter U as var used 2 times letter X as var used 1 times letter a as var used 96 times letter b as var used 43 times letter c as var used 144 times letter d as var used 84 times letter e as var used 24 times letter f as var used 172 times letter g as var used 40 times letter h as var used 53 times letter i as var used 245 times letter j as var used 71 times letter k as var used 54 times letter l as var used 42 times letter m as var used 124 times letter n as var used 176 times letter o as var used 24 times letter p as var used 76 times letter q as var used 28 times letter r as var used 67 times letter s as var used 300 times letter t as var used 82 times letter u as var used 11 times letter v as var used 66 times letter w as var used 42 times letter x as var used 64 times letter y as var used 37 times letter z as var used 10 times all used one letter as var 2236 times
For Django dir in my pc.
Output:
letter A as var used 1 times letter D as var used 1 times letter L as var used 1 times letter M as var used 1 times letter T as var used 1 times letter c as var used 28 times letter d as var used 13 times letter e as var used 1 times letter f as var used 16 times letter g as var used 23 times letter h as var used 2 times letter i as var used 15 times letter j as var used 3 times letter k as var used 1 times letter m as var used 27 times letter n as var used 5 times letter p as var used 13 times letter q as var used 4 times letter r as var used 14 times letter s as var used 48 times letter t as var used 27 times letter u as var used 7 times letter v as var used 8 times letter w as var used 1 times letter x as var used 5 times letter y as var used 2 times all used one letter as var 268 times
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Annoyance - Newer version of pygame nilamo 22 619 Sep-29-2018, 03:03 AM
Last Post: metulburr

Forum Jump:


Users browsing this thread: 1 Guest(s)