Simple CLIs in Python

Jacob Lam
Jacob Lam
Nov 4 · 3 min read

This week I decided to devote time to relearning python, and what better way to learn than making some projects. I wanted to start small, and in the coming weeks develop and build more complex programs in python. The three CLIs that I made this week, included a number guessing game, RPS game, and a password generator.

Number Guessing Game

I broke my CLI into two sections, the first of which is set the difficulty as well as the variables in the game. In this section I also made use of the random library to generate a random number for the player to guess within the difficulty range .

import randomprint('Welcome to the Guessing Game')# receives user difficulty and saves a variable
diff = input('Choose your difficulty: ')
# randomly generates number, using difficulty to set dynamic range
mystery_num = random.randint(1,diff * 10)
# variables
guess = None
count = 0
low = '0'
high = str(diff * 10)

The second section was a combination of while loops and if statements, continually asking the player what their guess is until they either guess the number or they run out of guesses.

while mystery_num != guess :guess = input('Guess a number between ' + low + ' - ' + high + " : " , )    if guess > mystery_num :
print('Guess lower')
high = str(guess)
count += 1
elif guess < mystery_num :
low = str(guess)
print('Guess higher')
count += 1
else :
print('You Win!')
if count == 3 :
print('You Lose!')
break

RPS Game

For the RPS CLI, I am going to be showing my refactored code. My first attempt I brute forced my way through the code, writing out every possibility. This solution was very tedious, however this gave me more practice using comparative operators in python. My second implementation utilized a dictionary, the key as the players choice, and the value being a dictionary containing the computers choice as well as the result. This resulted in a more elegant solution.

import randomprint('welcome to rock paper scissor')choices = ['rock','paper','scissor']
score = 0
playing = True
outcomes = {
'rock': {
'rock': 'tie',
'paper': 'lose',
'scissor': 'win'
},
'paper': {
'rock': 'win',
'paper': 'tie',
'scissor': 'lose'
},
'scissor': {
'rock': 'lose',
'paper': 'win',
'scissor': 'tie'
}
}
while playing == True :
# players picking what they want
player_choice = raw_input('Choose rock, paper, or scissor: ')
comp_choice = random.choice(choices)
if outcomes[player_choice][comp_choice] == 'win':
score += 1
print('You Win!', score )
elif outcomes[player_choice][comp_choice] == 'tie':
print('You tied!', score )
elif outcomes[player_choice][comp_choice] == 'lose':
print('You lose!')
playing = False
print('Your score: ', score )

Password Generator

While this CLI was very similar to my other projects. The major take away that I learned from this project was the difference between input() and raw_input(). Initially I used input(), however when I went to test my code. It would return :

NameError: name 'y' is not defined

What the program was trying to do was set the variable to the variable y, which has not been defined. I could have the user type:

$python passgen.py Do you want letters? (y/n): "y"

And this would set the variable to the string y, however a user would not generally think of typing “y”. By using raw_input() , the program will assume that the input is a string or an integer. Allowing the player to simply type y instead of “y”.

import randomprint('Password Generator')# password bank
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
symbols = "!@#$%^&*()?"
numbers = "0123456789"
# ask for password criteria
passlength = input('How long of a password do you need? ')
letters_bool = raw_input('Do you want letters? (y/n): ')
numbers_bool = raw_input('Do you want numbers? (y/n): ')
symbols_bool = raw_input('Do you want symbols? (y/n): ')
if letters_bool == 'y' :
pool = "".join(letters)
if numbers_bool == 'y' :
pool = pool + numbers
if symbols_bool == 'y' :
pool = pool + symbols
password = "".join(random.sample(pool, passlength ))print password

Jacob Lam

Written by

Jacob Lam

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade