patternpythonModerate
PyDOS: Version 2.0
Viewed 0 times
versionpydosstackoverflow
Problem
This is a follow up question to PyDOS shell simulation.
You might remember PyDOS from my other post. I have now updated it and it's better than ever! Post any ideas below that could make it better.
```
import time
import os
import sys
import random
def splash():
print ()
print ("PyDOS")
print ()
print ("Version 1.9.8")
print ()
time.sleep(3.2)
def calc():
while True:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("1: Addition\n2: Subtraction\n3: Multiplacation\n4: Division")
suggestion = input("Your Choice: ")
if suggestion == "1":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1+num2
print ("Your answer is:")
print (answer)
break
if suggestion == "2":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1-num2
print ("Your answer is:")
print (answer)
break
if suggestion == "3":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1*num2
print ("Your answer is:")
print (answer)
break
if suggestion == "4":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1/num2
print ("Your answer is:")
print (answer)
break
else:
print ("Your operation choice is invalid!")
def get_lines():
print("Enter 'stop' to end.")
lines = []
line = input()
while line != 'stop':
lines.append(line)
line = input()
return lines
def textviewer():
os.
You might remember PyDOS from my other post. I have now updated it and it's better than ever! Post any ideas below that could make it better.
```
import time
import os
import sys
import random
def splash():
print ()
print ("PyDOS")
print ()
print ("Version 1.9.8")
print ()
time.sleep(3.2)
def calc():
while True:
print ("Welcome to PyCALC")
print ("Please type the number for your suggestion.")
print ("1: Addition\n2: Subtraction\n3: Multiplacation\n4: Division")
suggestion = input("Your Choice: ")
if suggestion == "1":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1+num2
print ("Your answer is:")
print (answer)
break
if suggestion == "2":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1-num2
print ("Your answer is:")
print (answer)
break
if suggestion == "3":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1*num2
print ("Your answer is:")
print (answer)
break
if suggestion == "4":
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
print ("Please wait...")
time.sleep(0.6)
answer = num1/num2
print ("Your answer is:")
print (answer)
break
else:
print ("Your operation choice is invalid!")
def get_lines():
print("Enter 'stop' to end.")
lines = []
line = input()
while line != 'stop':
lines.append(line)
line = input()
return lines
def textviewer():
os.
Solution
Beautiful is better than ugly
Your calculator is ugly. @Hosch gave some hints but a much more dramatic redesign is needed. Why must the user say a number that is mapped to a symbol that is mapped to an operation only then applied to the numbers? This is user unfriendliness taken to the extreme. I suggest this very primitive but much more sane implementation:
Now the user interface is:
Readability counts
To a poor average human this is just noise:
You have been told to make a function out of this.
Ironically enough, you defined a (badly named) function
First class bug
Be lazy
The good programmer is lazy, he/she never does by hand what could be automated:
Let's say you add another utility or change the functionality of one, will you remember to update this list? Maybe.
This list can be generated by using a dictionary but I will not give you the fish.
Using
You may want to change some of your longer strings, looking for them inside the code is not the best option. Instead define them as constants at the start of your file:
you will then
The master directory
Why this directory? Why can't the user change this? He/she may want to change the location where the files are saved.
You are still sleeping
I said this, others said this, but I will repeat it:
sleeping does not make the terminal feel retro, it is seriously annoying!
Dealing with files with
Repetita iuvant // repeating helps
it.
The commands dictionary
Please go back to my previous answer and re-read this part: it is very important.
Your calculator is ugly. @Hosch gave some hints but a much more dramatic redesign is needed. Why must the user say a number that is mapped to a symbol that is mapped to an operation only then applied to the numbers? This is user unfriendliness taken to the extreme. I suggest this very primitive but much more sane implementation:
def calc():
print ("Welcome to PyCALC")
while True:
expr = input("> ")
a,oper,b = expr.split()
operations = {'+': op.add,
'-': op.sub,
'*': op.mul,
'/': op.truediv
}
print(operations[oper](int(a),int(b)))Now the user interface is:
> 2 + 2
4Readability counts
To a poor average human this is just noise:
os.system('cls' if os.name == 'nt' else 'clear')You have been told to make a function out of this.
Ironically enough, you defined a (badly named) function
cls but you did not use it ?!First class bug
lines = get_linesget_lines is a function... you are saving the content of a function in the text file... calling a function is done by applying double parenthesis get_lines()Be lazy
The good programmer is lazy, he/she never does by hand what could be automated:
print ("dir - displays directory.")
print ("cls - clears screen")
print ("help - shows help")
print ("textviewer - launches textviewer")
print ("edit - launches edit")
print ("news - launches news")
print ("shutdown - closes PyDOS")
print ("calc - launches calculator")Let's say you add another utility or change the functionality of one, will you remember to update this list? Maybe.
This list can be generated by using a dictionary but I will not give you the fish.
"Please use {}".format(".format()")print("This command or file: "+shell+ " does not exist. Check for spelling errors and try again.\nIf you are trying to open a textfile, open textviewer.")Using
{}.format inside strings is common practice, do not use +CONSTANTly improving maintainabilityYou may want to change some of your longer strings, looking for them inside the code is not the best option. Instead define them as constants at the start of your file:
NEWS = """"PyDOS NEWS"
"New Additions:"
"New calculator app!"
"All text documents from edit are now stored in a 'files' directory"
"Tweaks and fixes:"
"BUG023L: Fixed issue with splash screen loop"
"Reported Bugs:"
"BUG024T: Hangman returns a traceback error, we are fixing
"BUG025T: Pressing 'y' in texteditor when it asks you if you want\nto edit the file returns Type_Error"
"""you will then
print(NEWS)The master directory
"/media/GENERAL/Projects/files"Why this directory? Why can't the user change this? He/she may want to change the location where the files are saved.
You are still sleeping
I said this, others said this, but I will repeat it:
sleeping does not make the terminal feel retro, it is seriously annoying!
Dealing with files with
withRepetita iuvant // repeating helps
with is a nicer way of dealing with files, even the official docs recommendit.
The commands dictionary
Please go back to my previous answer and re-read this part: it is very important.
Code Snippets
def calc():
print ("Welcome to PyCALC")
while True:
expr = input("> ")
a,oper,b = expr.split()
operations = {'+': op.add,
'-': op.sub,
'*': op.mul,
'/': op.truediv
}
print(operations[oper](int(a),int(b)))os.system('cls' if os.name == 'nt' else 'clear')lines = get_linesprint ("dir - displays directory.")
print ("cls - clears screen")
print ("help - shows help")
print ("textviewer - launches textviewer")
print ("edit - launches edit")
print ("news - launches news")
print ("shutdown - closes PyDOS")
print ("calc - launches calculator")print("This command or file: "+shell+ " does not exist. Check for spelling errors and try again.\nIf you are trying to open a textfile, open textviewer.")Context
StackExchange Code Review Q#85866, answer score: 13
Revisions (0)
No revisions yet.