Sign in to follow this  
Kevin

Python Beginner Tutorial 7 - Clean Code and Exceptions

Recommended Posts

 

This tutorial covers an introduction to writing clean code. This video focuses on cleaning up our existing calculator program to make it more robust and easier to read. This video also introduces the for loop, loop control keywords "break," "continue" and introduces the "import" keyword to demonstrate time.sleep() and random.randint(). After watching this video you should be ready to start on the two challenges posed at the end of the video.

Challenge 1: Random Calculator

Modify the calculator program to use randint to run random calculations. Make this as random as you want so be creative!! Feel free to also use time.sleep() to give your program a more autonomous feel to it. Bonus points if you incorporate a for loop and loop control keywords. Remember to use the clean code strategies discussed in this video; if you have big blocks of code that look hard to read then try to fix it.

 Challenge 2: Rock, Paper, Scissors Game

This idea was originally created by Zibzo and what he is posted here. Try to write clean code and note that what was posted by Zibzo has room for improvement given what we learned in this video. 

 

Share this post


Link to post
Share on other sites

"""Returns num1 plus num2."""
    return def """Returns num1 minus num2."""
    return def """Returns num1 times num2."""
    return def """Returns num1 divided by num2."""
    return def """Allows user to run basic calculator operations with two numbers."""
    False
    True
    while while not # Get user input
            tryintinput"What is number 1?"intinput"What is number 2?"intinput"What do you want to do? 1. add, 2. subtract, 3. multiply, or 4. divide. Enter  number: "True
            exceptprint"Invalid input. Try again."# Determine operation
        if 1print"Adding..."printelif 2print"Subtracting..."printelif 3print"Multiplying..."printelif 4print"Dividing..."printelseprint"I don't understand"input'still want to run? y for yes/ n for no'if 'y'print"go ahead"elif 'n'False

image.png

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this