HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

PyDOS Version 3.0

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
versionpydosstackoverflow

Problem

This is a follow up to the questions: PyDOS shell simulation and PyDOS: Version 2.0

Introducing PyDOS 3.0! With brand new features and improved code, this will blow your mind!

New Features (This list will be updated with PyWrite soon)

-
Dialogs are now in a window like design.

-
New start-screen, easier to use.

-
Create your own name and change it!

-
New Commands Dictionary

-
PyCALC is now called SimpleCalc

If you have any ideas for improvements, don't hesitate to post them!

Source Code:

```
#PyDOS Version 3.0 - Written by Mrfunny744
import time
import os
import sys
import random

def ChangeName():
os.system('cls' if os.name == 'nt' else 'clear')
print ("----------------------------------------------")
name = input ("Type in your new username: ")
print()
print ("This can be changed again if you wish.")
print ("----------------------------------------------")
time.sleep(2.5)

def shutdown():
print ("------------------------")
print ("| |")
print ("| Thank you for using |")
print ("| PyDOS |")
print ("| |")
print ("| |")
print ("| |")
print ("| Shutting Down... |")
print ("| |")
print ("------------------------")
sys.exit(0)

def end():
print ("------------------------------------")
print ("| |")
print ("| Thanks for using SimpleCalc! |")
print ("| |")
print ("------------------------------------")
time.sleep(2)

def calc_a():
print ("----------------------------------")
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("----------------------------------")
answer = num1+num2
print ("Your answer is:" ,answer)
print ("----------------------------------")
time.sleep(3)
end()

def calc_m():
pri

Solution

time.sleep(2.5)


Again, don't sleep your program, it makes it unnecessarily slow, and it doesn't even act like an old machine would because when it does run, it runs quite fast.

--------------------------
|                        |
| Welcome to SimpleCalc  |
|                        |
| A = Add                |
| M = Multiply           |
| S = Subtract           |
| D = Divide             |
| Q = Quit               |
|                        |
--------------------------


You prompt for a capital "A", but only accept a lowercase "a":

if choice == 'a':
    calc_a()


You can easily change this to accept both like this:

choice = input ("| Choice:                |").lower()


If you want to accept any input that starts with an "A" or "a", just append [0] to the line.

Your various calc_x() functions are extremely similar. You can reduce some of that similarity by either passing them an argument or using a function to input the values. Personally, if you are not going to accept entire equations and handle order of operations, I would use one function:

def calc(operation):
    print ("----------------------------------")
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter a number: "))
    print ("----------------------------------")

    if operation == 'a':
        answer = num1 + num2
    elif operation == 's':
        answer = num1 - num2
    elif operation == 'm':
        answer = num1 * num2
    elif operation == 'd':
        answer = num1 / num2

    print ("Your answer is:" ,answer)
    print ("----------------------------------")
    end()


Then, call this function from simplecalc like this:

if choice in "asmd":
    calc(choice)
else:
    print ("Invalid specifacation.")    // "specification"


Don't call end(), or any other functions, from your calc() function. Actually, you shouldn't even print from your calc() function - what if you want to calculate something and not print it? Also, inputting values is not part of the calculation. I would write this like this (extraneous output removed for simplicity):

def calc(operation, num1, num2):
    if operation == 'a':
        return num1 + num2
    elif operation == 's':
        return num1 - num2
    elif operation == 'm':
        return num1 * num2
    elif operation == 'd':
        if num2 == 0:
            raise ValueError('Divide by 0 expection')

        return num1 / num2
    else:
        raise ValueError('Unknown error')

def simplecalc():
    print ("Welcome to SimpleCalc! You have the following operators:")
    print ("A: Add")
    print ("S: Subtract")
    print ("M: Multiply")
    print ("D: Divide")
    print ("Q: Quit")

    choice = input ("Choice: ").lower()[0]

    if choice == 'q':
        end_simple_calc()
        return

    if choice not in "asmd":
        print ("Invalid specification.")
        end_simple_calc()
        return

    try:
        num1 = int(input("Enter a number: "))
        num2 = int(input("Enter a number: "))
        print ("Your answer is:", calc(choice, num1, num2))

    except ValueError as err:
        print(err.args)

    end_simple_calc()


This not only catches the invalid division by 0 operation, it also catches when you input an invalid number and prints an error message instead of the printing stack trace and crashing.

Right here, you only allow three apps; why are the other two in the prompt list?

command_actions = {
'ChangeName' : ChangeName,
'SimpleCalc' : simplecalc,
'Shutdown' : shutdown
}


You should be consistent in your naming:

def ChangeName():
def shutdown():


I believe the PEP8 standard states you should use snake_case names.

Also related to naming: def end(). What does end() do? It prints a conclusion to SimpleCalc, but does not show that in the name. What if I was looking through your code adding a new feature, and I saw that in SimpleCalc? I might add it to the end of my new feature and have a problem.

Code Snippets

time.sleep(2.5)
--------------------------
|                        |
| Welcome to SimpleCalc  |
|                        |
| A = Add                |
| M = Multiply           |
| S = Subtract           |
| D = Divide             |
| Q = Quit               |
|                        |
--------------------------
if choice == 'a':
    calc_a()
choice = input ("| Choice:                |").lower()
def calc(operation):
    print ("----------------------------------")
    num1 = int(input("Enter a number: "))
    num2 = int(input("Enter a number: "))
    print ("----------------------------------")

    if operation == 'a':
        answer = num1 + num2
    elif operation == 's':
        answer = num1 - num2
    elif operation == 'm':
        answer = num1 * num2
    elif operation == 'd':
        answer = num1 / num2

    print ("Your answer is:" ,answer)
    print ("----------------------------------")
    end()

Context

StackExchange Code Review Q#88502, answer score: 11

Revisions (0)

No revisions yet.