In this video (in Portuguese) I show how to create a very simple command-line interface (CLI) in Python and, along the way, how to use branches in Git.
It is very common for beginner programming materials to tell students to write programs that ask the user for some information, process it, and display the result on screen. For simplicity, such programs are usually command-line programs that run in the terminal. However, when you deal with input straight from the user, some care is needed. In this video I show a few ways to handle wrong user input and, along the way, I introduce a few more Git concepts, such as the use of branches.
The video starts from the program created earlier here, where I show how to compute the factorial of a number in Python with just one line.
The links to the sites and other materials mentioned in the video are right after the video player:
Repository of the Rich project for better formatting in the terminal.
Repository of the Oh My Zsh project, a configuration framework for Zsh.
In the video, I show the following generic structure for a very simple Python CLI:
if __name__ == '__main__':
while True:
try:
variable = input('Text asking for something: ')
# code that does the initial processing of what was received in the variable
except ValueError:
print('Error message for the user')
# create as many except clauses as needed for the cases that may occur in your code
except KeyboardInterrupt:
print(' Execution ended by the user.')
break
else:
print('Message for the user with the result')
# Edit the message above to show whatever you want to the user.
# Create more prints if needed.