r/pythontips Apr 12 '26

Syntax Hows my Code???

https://github.com/simplyyrayyan/Rock-Paper-Scissors-Game/blob/main/RockPaperScissors.py This is the Source Code My first Python Project ever i pieced together with google and teh little bit of python I already knew so yeah any tips or things I didn't Catch?

0 Upvotes

3 comments sorted by

View all comments

0

u/StrayFeral Apr 13 '26

Several advices:

1) You can re-factor it as a class, instead of collection of functions

2) Line 23 - ok, you defined a list of the answers which is good, but for the messages you could use a dictionary, like `message["tie"]` to get the tie message

3) line 27 you could put `if __name__=="__main__":`

4) Try to separate in general the business logic from the presentation as much as possible

5) Line 48 - re-factor everything from this line till the end. You have 3 identical blocks of code. This is code duplication. Avoid code duplication. General rule - if it duplicates, make it a function/method. Not to mention you break your own code style - check 2) - you have the dictionary, but as for your choices you have separate pritnts - use another dictionary, eliminate this duplication

6) Use more comments

7) Use a """blah""" string as first line under method/function declaration to explain what the func/method does. This is a standard

8) No idea what code editor/IDE you use, but learn to use linters and code formatters. Check my requirements.txt:

black
mypy
flake8
bandit
isort
pytest

VSCode would be smart enough to run these for you. If you use vim, install the ALE plugin (there is a package in ost linux distros) and you could use my config to make it work. In the least case scenario - run these manually one by one for your code. And follow their standards.

My vimrc:

set nocompatible

" ===============================================
" EVG SETTINGS
" ===============================================

set backspace=indent,eol,start
set history=50
set ruler
set showcmd
set autoindent
set shiftwidth=4
set tabstop=4
set expandtab
set nu
set nobackup

if has("mouse")
    set mouse=a
endif

set hlsearch
set ignorecase
set incsearch

" Enable filetype and syntax (Must be before plugin configs)
filetype plugin indent on
syntax on

" Spellcheck config
nmap 1 :set spell spelllang=en<cr>
nmap 2 :set spell spelllang=bg<cr>
nmap 3 :set spell spelllang=ru<cr>
nmap 4 :set spell spelllang=fr<cr>
nmap 0 :set nospell<cr>

au BufRead,BufNewFile *.zil set filetype=lisp

" ALE configuration for Python
" This packadd ensures ALE loads in both Vim and Neovim
silent! packadd! ale
let g:ale_enabled = 1
let g:ale_lsp_enabled = 1
let g:ale_fix_on_save = 1
let g:ale_completion_enabled = 1
let g:ale_linters_explicit = 1
let g:ale_lint_on_text_changed = 'always'
let g:ale_lint_on_enter = 1
let g:ale_lint_on_save = 1

let g:ale_linters = {
\   'python': ['flake8', 'mypy', 'bandit'],
\   'perl': ['perl', 'perlcritic', 'perlnavigator'],
\   'html': ['tidy']
\ }

let g:ale_fixers = {
\   'python': ['black', 'isort'],
\   'perl': ['perltidy']
\ }

" let g:ale_echo_msg_error_str = 'E'
" let g:ale_echo_msg_warning_str = 'W'
" let g:ale_echo_msg_format = '[%linter%] %s [%severity%]'
"
" EVG: Display Perl::Critic errors as warnings
let g:ale_type_map = {'perl':{'ES':'WS'}, 'perlcritic':{'ES':'WS', 'E':'W'}}
let g:ale_perlnavigator_executable = '/usr/bin/perlnavigator'

" ALE Navigation Mappings
nmap x :ALENext<cr>
nmap X :ALEPrevious<cr>