Skip to main content
typo
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good thethey feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

Be helpful

It is common and good practice to show some help/info if the user types in help.

Who sleeps totoo much does not catch fish annoys the user

Why do you sleep up to 4 seconds?

time.sleep(4)

Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest time.sleep(0.5)

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

Be helpful

It is common and good practice to show some help/info if the user types in help.

Who sleeps to much does not catch fish annoys the user

Why do you sleep up to 4 seconds?

time.sleep(4)

Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest time.sleep(0.5)

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good they feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

Be helpful

It is common and good practice to show some help/info if the user types in help.

Who sleeps too much does not catch fish annoys the user

Why do you sleep up to 4 seconds?

time.sleep(4)

Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest time.sleep(0.5)

typo
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

Be helpful

It is common and good practice to show some help/info if the user types in help.

Who sleeps to much does not catch fish annoys the user

Why do you sleep up to 4 seconds?

time.sleep(4)

Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest time.sleep(0.5)

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

Be helpful

It is common and good practice to show some help/info if the user types in help.

Who sleeps to much does not catch fish annoys the user

Why do you sleep up to 4 seconds?

time.sleep(4)

Ok, this emulates an ancient terminal but really, you don't want to make the user wait so much without reason. I suggest time.sleep(0.5)

if
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

5 lines ought to be enough for everyone

The following has no meaning. Why 5?

line1 = input()
line2 = input()
line3 = input()
line4 = input()
line5 = input()

Instead let the user end:

def get_lines():
    print("Enter 'END' to end.")
    lines = []
    line = input()
    while line != 'END':
         lines.append(line)
         line = input()
    return lines

Now you can write:

lines = get_lines()
file.write('\n'.join(lines))

Programming with with

with is an extremely useful idiom because it handles closing files automagically.

with open(filename,'r') as f:
    f.read()
# The file is closed automatically :)

If considered harmful (if overused)

So you just started programming and think "Oh the if statements are so good the feel so CS" (I felt that way too when I started programming when I was 15 so I understand you).

BUT so many if and elif are repetitive and you should not repeat yourself.

I would suggest the high level dictionary to accomplish the task more shortly.

command_actions = {
    'textviewer' : textviewer,
    'edit' : edit,
    'dir' : print_dir,
    'cls' : clear_screen,
    'shutdown' : shutdown,
    'help' : give_help
    }
if shell in command_actions:
    action = command_actions[shell]
    action()
else:
    error_message()
Source Link
Caridorc
  • 28.2k
  • 7
  • 55
  • 138
Loading