He returns from his time away... This time with something vaguely interesting!.....

B

blackneos940

Guest
I'VE DONE IT!!...... :D FINALLY!!..... :D After many nights of Aspergers, Depression, some existential Anxiety, and doubt about my own Programming skills, I made ANOTHER Program, written in Python.....!! ^^ And soon, I'll get some much-needed rest..... :3 (Note to self: NEVER write Comments while on no sleep.... :()
Code:
#Here, we need to use the time Module for, well, time-related things like counting down, etc., etc.

#We also need the os Module for things like writing to a Directory of the user's choosing, and for clearing the screen, which ALSO depends on which OS the user is, well, using :)

#This Program, however awful, is released under the GNU GPL V1, or, if God instills the desire in you, any later version... Yes, I haven't slept all night... :3

import time as t

import os

print ("Welcome to Swiss: The Smarter Toolkit!\n");

#We get input from the user on what to do first

task = (input("You can do one of three things.\nFor now...\n\n1. Create a random file\n2. Use a pretty l337 timer.\n3. Stream an endless barrage of a word of your choice. Don't be filthy, now... ;)\nEnter a choice now. Please use numbers 1-3, or else this l337 Program will crap out in a totally uncool way... :'(\n>>> "))

if task == "1":

  file_name = input("Name teh file!! :3 Make SURE you add the appropriate extension now! :3\n\n>>> ")

  directory_choice = input("Type in the directory you wish to save said File in! :3\ Depending on your OS of choice, you'll have to use either a \"/\" or a \"\\\"\n>>> ")

  os.chdir()

  with open (file_name, "w") as output:
  
    output.write(input("Ah, the Literary sort, are we...? Type whatever you feel like typing into the following Prompt now. It could be, oh, I don't know, a Shell Script, a Batch File, or some other life-ruining, evil piece of Code... ;3\n\n>>> "))
  
if task == "2":

  number_of_secs = int(input("How many Seconds do you want to count down with...? Anything other than seconds, and it could get bad...\n>>> "))

  t.sleep(number_of_secs)

  print ("Times Up! :3 Huh, my arm and chest both hurt... Tingling.... Dear God. O.o\nNo... Not time to die YET!! :D I'm too LAZY to die! :D")

if task == "3":

  #Here's what determines which OS's command we use. (I.E., "cls" in Windows, and "clear" in Linux and OSX)

  Operating_System = input("Which OS are you using?\nType either \"Linux\", \"OSX\", or \"Windows\", EXACTLY as shown. Otherwise, it won't work.\nAlso, if you are using Cygwin, just type in Linux. I found that out while crash-testing this Program. :)\n>>> ")

  pandoras_box = input("Enter your gibberish now!\nIt can be about ANYTHING you want... You can even insult the CREATOR of this Program! :D\n>>> ")

  while True != False:

    #Ooh, FANCY.....  :3

    t.sleep(1)

    print (pandoras_box)
  
    if Operating_System == "Windows":
  
      os.system("cls")
  
    elif Operating_System == "Linux":
    
      os.system("clear")
    
    elif Operating_System == "OSX":
    
      os.system("clear")

  #This is where we finish up.

print ("Thank you for trying out this heap of s-- Er, thank you for trying out this Program! Have a great day!!\n")
 


Congratulations! Although reading code is (almost) like reading Greek to me, I am happy to watch you learn and progress. Keep it up!
 
I see you are learning. Here are a few suggestions that might help you to improve your program a little and also improve your knowledge (if you can be bothered to read yet another wall of text from me!):

1. You forgot to use a shebang at the very first line of the script to tell the terminal which type of interpreter to use.
e.g.
Code:
#!/usr/bin/env python3
Specifying which interpreter to use via a shebang is good practice anyway, but even so: As it currently stands, you should note that your python script will only work on Python 3 because of the way you are using the 'input' command and the way that you are checking the input. So specifying the interpreter with a shebang will make it clear to the Terminal and to any other users that read your script that it is a Python3 script and NOT Python2!

To explain about what I mean about your use of the 'input' keyword:
In python3.x the 'input' keyword is equivalent to the python2.x 'raw_input' keyword, which always stores the users input as a String. In python3.x if you want the users input to be stored as a numeric value, you have to cast it to the type you want.

e.g. To get an integer in Python3, you would use this:
Code:
x = int(input("Enter an integer:"))
Without casting, you always get a String value back from 'input'.

Whereas the python2.x 'input' function would always try to evaluate the value entered by the user and would determine whether it was a string or a numeric value and automatically store it in the variable as whatever type it decides is best. Which isn't necessarily always what the programmer wants.
So for Python 3, they decided to drop the old python2.x 'input' keywords functionality and made the python3.x version of 'input' behave the same as python2.x's 'raw_input' command.

Because of these subtle differences in the operation of the 'input' function in python2 and python3; if you run your script in python2.x, it will not work properly. In python2.x, the if statements in your script that compare the variable 'task' against the String values "1", "2" and "3" will fail. Because under python2; if the user enters 1, 2 or 3, 'task' will actually contain integer values, not string values - thanks to python2's implementation of input; which as described, automagically stores purely numeric input values as number types rather than as a String. So when you compare an int against a string, the test fails. The integer value 1 is not the same as the String value "1".

Hope that wasn't too confusing!
BTW: Your script is absolutely fine as a Python3 script, I'm just letting you know that it is not 100% compatible with python2 and will not run properly under python2. If you have a shebang at the start of the script that specifies python3, then it is obvious to anybody who looks at the script that it is a python3 script. Also any terminals that actually process shebangs will also be able to determine which interpreter to use and will use python3 instead of automatically using python2 (which is still the default version of Python on most Linux distros). Phew! That was an effort!

OK, onto the rest of the script....

2. Task1 - Creating a file:
You appear to have forgotten to do anything with the directory you asked the user to enter (the one stored in the variable 'directory_choice'). Shouldn't this variable be used in the call to os.chdir()?
e.g.
Code:
os.chdir(directory_choice)

In fact, before attempting to go into the user-specified directory and write a file, it is probably an idea to make sure that the user has entered a valid path that exists. After all - users are stupid, sneaky creatures and cannot be trusted to enter sensible values into programs!

Luckily, you have functions in the os.path module that you can use to check this - and you have already imported the os module, so we may as well use it to its full effect:
Code:
if os.path.exists(directory_choice) and os.path.isdir(directory_choice):
    os.chdir(directory_choice)
    with open(file_name, "w") as output:
        output.write(input("Ah, the literary....{Long prompt text here}.... >>>"))
The above code uses the os.path module to determine whether the path is valid/exists and that the final destination of the path is a directory (and not a file, or something else!). As long as the user entered a valid path that exists and is a directory; the os.chdir function is used to change directory into the target dir and we write the line of text that the user enters into a new file in that directory.

3. Task 3 - Endless barrage of text:
Rather than getting the user to enter the OS name, you could simply use os.name. os.name will give you the name of the OS the script is running on.
Again, you have already imported the os module, so you may as well use it!
os.name will return one of the following values depending on the OS:
'posix', 'nt', 'mac', 'os2', 'ce', 'java'
'nt' is the name that gets returned for windows terminals, 'posix' is used for Unix/Linux/Cygwin and 'mac' is obviously mac.

Mac, Cygwin and Unix/Linux use the clear command to clear the screen, so you really only need to do something different if the current os is windows.
So you can get rid of the part where you prompt the user enter the OS and simply detect it in your script where you are trying to clear the screen.
Like this:
Code:
if os.name == "nt":
    os.system("cls")
else:
    os.system("clear")

4. Task 3 - Endless barrage of text:
A very minor nit-picky thing - your infinite while loop doesn't need the "!=False" condition:
Code:
while True != False:
Could just be this:
Code:
while True:

I hope I've explained things clearly enough. If nothing else, you've learned a few new things about the os module.
e.g.
- How to use the os.path module to check whether directories exist and actually ARE directories AND NOT files, or links, or small, off-duty Czechoslovakian traffic wardens etc etc.
- How to use os.name to determine the platform being used, before attempting to do platform specific things like clearing the screen!

And in doing so, you have also made your program more robust and less likely to crash! As ever, if you have any problems, confusions, questions etc, do not hesitate to ask! :)
 
I see you are learning. Here are a few suggestions that might help you to improve your program a little and also improve your knowledge (if you can be bothered to read yet another wall of text from me!):

1. You forgot to use a shebang at the very first line of the script to tell the terminal which type of interpreter to use.
e.g.
Code:
#!/usr/bin/env python3
Specifying which interpreter to use via a shebang is good practice anyway, but even so: As it currently stands, you should note that your python script will only work on Python 3 because of the way you are using the 'input' command and the way that you are checking the input. So specifying the interpreter with a shebang will make it clear to the Terminal and to any other users that read your script that it is a Python3 script and NOT Python2!

To explain about what I mean about your use of the 'input' keyword:
In python3.x the 'input' keyword is equivalent to the python2.x 'raw_input' keyword, which always stores the users input as a String. In python3.x if you want the users input to be stored as a numeric value, you have to cast it to the type you want.

e.g. To get an integer in Python3, you would use this:
Code:
x = int(input("Enter an integer:"))
Without casting, you always get a String value back from 'input'.

Whereas the python2.x 'input' function would always try to evaluate the value entered by the user and would determine whether it was a string or a numeric value and automatically store it in the variable as whatever type it decides is best. Which isn't necessarily always what the programmer wants.
So for Python 3, they decided to drop the old python2.x 'input' keywords functionality and made the python3.x version of 'input' behave the same as python2.x's 'raw_input' command.

Because of these subtle differences in the operation of the 'input' function in python2 and python3; if you run your script in python2.x, it will not work properly. In python2.x, the if statements in your script that compare the variable 'task' against the String values "1", "2" and "3" will fail. Because under python2; if the user enters 1, 2 or 3, 'task' will actually contain integer values, not string values - thanks to python2's implementation of input; which as described, automagically stores purely numeric input values as number types rather than as a String. So when you compare an int against a string, the test fails. The integer value 1 is not the same as the String value "1".

Hope that wasn't too confusing!
BTW: Your script is absolutely fine as a Python3 script, I'm just letting you know that it is not 100% compatible with python2 and will not run properly under python2. If you have a shebang at the start of the script that specifies python3, then it is obvious to anybody who looks at the script that it is a python3 script. Also any terminals that actually process shebangs will also be able to determine which interpreter to use and will use python3 instead of automatically using python2 (which is still the default version of Python on most Linux distros). Phew! That was an effort!

OK, onto the rest of the script....

2. Task1 - Creating a file:
You appear to have forgotten to do anything with the directory you asked the user to enter (the one stored in the variable 'directory_choice'). Shouldn't this variable be used in the call to os.chdir()?
e.g.
Code:
os.chdir(directory_choice)

In fact, before attempting to go into the user-specified directory and write a file, it is probably an idea to make sure that the user has entered a valid path that exists. After all - users are stupid, sneaky creatures and cannot be trusted to enter sensible values into programs!

Luckily, you have functions in the os.path module that you can use to check this - and you have already imported the os module, so we may as well use it to its full effect:
Code:
if os.path.exists(directory_choice) and os.path.isdir(directory_choice):
    os.chdir(directory_choice)
    with open(file_name, "w") as output:
        output.write(input("Ah, the literary....{Long prompt text here}.... >>>"))
The above code uses the os.path module to determine whether the path is valid/exists and that the final destination of the path is a directory (and not a file, or something else!). As long as the user entered a valid path that exists and is a directory; the os.chdir function is used to change directory into the target dir and we write the line of text that the user enters into a new file in that directory.

3. Task 3 - Endless barrage of text:
Rather than getting the user to enter the OS name, you could simply use os.name. os.name will give you the name of the OS the script is running on.
Again, you have already imported the os module, so you may as well use it!
os.name will return one of the following values depending on the OS:
'posix', 'nt', 'mac', 'os2', 'ce', 'java'
'nt' is the name that gets returned for windows terminals, 'posix' is used for Unix/Linux/Cygwin and 'mac' is obviously mac.

Mac, Cygwin and Unix/Linux use the clear command to clear the screen, so you really only need to do something different if the current os is windows.
So you can get rid of the part where you prompt the user enter the OS and simply detect it in your script where you are trying to clear the screen.
Like this:
Code:
if os.name == "nt":
    os.system("cls")
else:
    os.system("clear")

4. Task 3 - Endless barrage of text:
A very minor nit-picky thing - your infinite while loop doesn't need the "!=False" condition:
Code:
while True != False:
Could just be this:
Code:
while True:

I hope I've explained things clearly enough. If nothing else, you've learned a few new things about the os module.
e.g.
- How to use the os.path module to check whether directories exist and actually ARE directories AND NOT files, or links, or small, off-duty Czechoslovakian traffic wardens etc etc.
- How to use os.name to determine the platform being used, before attempting to do platform specific things like clearing the screen!

And in doing so, you have also made your program more robust and less likely to crash! As ever, if you have any problems, confusions, questions etc, do not hesitate to ask! :)
I knew about the "raw_input" thing, believe it or not..... :) Thanks for the advice, good sir!..... :D But, I forgot to put "int" before the "input" Function..... Or DID I.....?? o_O Anyway, I implemented your revisions into my Code..... :)

Code:
#Here, we need to use the time Module for, well, time-related things like counting down, etc., etc.

#We also need the os Module for things like writing to a Directory of the user's choosing, and for clearing the screen, which ALSO depends on which OS the user is, well, using :)

#This Program, however awful, is released under the GNU GPL V1, or, if God instills the desire in you, any later version... Yes, I haven't slept all night... :3

#!/usr/bin/env python3

import time as t

import os

print ("Welcome to Swiss: The Smarter Toolkit!\n");

#We get input from the user on what to do first

task = int(input("You can do one of three things.\nFor now...\n\n1. Create a random file\n2. Use a pretty l337 timer.\n3. Stream an endless barrage of a word of your choice. Don't be filthy, now... ;)\nEnter a choice now. Please use numbers 1-3, or else this l337 Program will crap out in a totally uncool way... :'(\n>>> "))

if task == 1:

  file_name = input("Name teh file!! :3 Make SURE you add the appropriate extension now! :3\n\n>>> ")

  directory_choice = input("Type in the directory you wish to save said File in! :3\ Depending on your OS of choice, you'll have to use either a \"/\" or a \"\\\"\n>>> ")

  if os.path.exists(directory_choice) and os.path.isdir(directory_choice):

    os.chdir(directory_choice)

    with open (file_name, "w") as output:

      output.write(input("Ah, the Literary sort, are we...? Type whatever you feel like typing into the following Prompt now. It could be, oh, I don't know, a Shell Script, a Batch File, or some other life-ruining, evil piece of Code... ;3\n\n>>> "))
  
  else:
  
    print ("There's no Directory of that on THIS Machine.")

if task == 2:

  number_of_secs = int(input("How many Seconds do you want to count down with...? Anything other than seconds, and it could get bad...\n>>> "))

  t.sleep(number_of_secs)

  print ("Times Up! :3 Huh, my arm and chest both hurt... Tingling.... Dear God. O.o\nNo... Not time to die YET!! :D I'm too LAZY to die! :D")

if task == 3:

  #Here's what determines which OS's command we use. (I.E., "cls" in Windows, and "clear" in Linux and OSX)")

  pandoras_box = input("Enter your gibberish now!\nIt can be about ANYTHING you want... You can even insult the CREATOR of this Program! :D\n>>> ")

  while True:

    #Ooh, FANCY.....  :3

    t.sleep(1)

    print (pandoras_box)

    if os.name == "nt":

      os.system("cls")

    else:

      os.system("clear")

  #This is where we finish up.

print ("Thank you for trying out this heap of s-- Er, thank you for trying out this Program! Have a great day!!\n")

So what do you think......? :3 I REALLY appreciate your input(), good sir..... :3 See what I did there.....? :3
 
So what do you think......? :3 I REALLY appreciate your input(), good sir..... :3 See what I did there.....? :3

Sorry, was going to reply yesterday, but for some reason linux.org appeared to be down. Not sure if there was an outage yesterday, or if it was just some DNS related problem on the network at my end.

Looks good, only thing is - your shebang needs to be the very first line in the file.

If you are using a terminal that supports the use of shebangs in scripts - which is basically any POSIX terminal that isn't Windows cmd or Powershell (these two ignore the shebang and just treat it as a comment); The shebang allows you to execute the script using:
Code:
./somepythonprogram.py
instead of having to use:
Code:
python3 ./somepythonprogram.py

But if the shebang is not in the very first line in the script; if you execute the script from the terminal using:
Code:
./somepythonprogram.py
The terminal/shell will not be able to automatically determine the interpreter to be used to execute the script and it will attempt to execute it as a shell-script instead and will then throw a bunch of errors at you!

So the shebang should always be the very first line in the file, before any other comments or code!
:)
 
Sorry, was going to reply yesterday, but for some reason linux.org appeared to be down. Not sure if there was an outage yesterday, or if it was just some DNS related problem on the network at my end.

Looks good, only thing is - your shebang needs to be the very first line in the file.

If you are using a terminal that supports the use of shebangs in scripts - which is basically any POSIX terminal that isn't Windows cmd or Powershell (these two ignore the shebang and just treat it as a comment); The shebang allows you to execute the script using:
Code:
./somepythonprogram.py
instead of having to use:
Code:
python3 ./somepythonprogram.py

But if the shebang is not in the very first line in the script; if you execute the script from the terminal using:
Code:
./somepythonprogram.py
The terminal/shell will not be able to automatically determine the interpreter to be used to execute the script and it will attempt to execute it as a shell-script instead and will then throw a bunch of errors at you!

So the shebang should always be the very first line in the file, before any other comments or code!
:)

Oh, so you think it was down.....?? :) I have a feeling that they had to do some maintenance, or perhaps remove the Imagination Virus..... :3 I'm evil.... :3 Quiver before me..... :D Anywho.... :3 I put the Shebang there..... :D I'll do it from now on..... :) I even have Cygwin on Windows!..... ^^ I'm gonna try and come here more.... It's just that sometimes I just..... Withdraw.... :( I'm sorry, jas.... :( Ah, so the Terminal NCURSES at you if you put the Shebang in the wrong part of the Code.... :3 Oh, I just Love puns....... :3 Ok!..... ^^ Well, I have insults enabled in Bash, so I can take the heat!..... :3 BTW, when I first Coded Swiss, I listened to Dubstep meant for Hacking..... Not much Hacking there, though the Program COULD be used to destroy the victims HDD, if they run Windows, or DVL..... o_O But, we all know that DVL is MEANT to have SCSI Security measures..... :3 But the title of the Audio File says: "Dubstep Epic (Gamers - _Code is Art_", so I assume it goes with ANY kinda Programmin'..... :3 It sure FELT epic..... :D One day, I'm gonna make a Program that displays a crap-ton of GNU Pictures if the User is using Windows....... :3 ('Course, that would happen on my ASUS, obviously..... XD) Thanks for the help, good sir!!!..... ^^
 
Oh, so you think it was down.....?? :) I have a feeling that they had to do some maintenance, or perhaps remove the Imagination Virus..... :3 I'm evil.... :3 Quiver before me..... :D Anywho.... :3 I put the Shebang there..... :D I'll do it from now on..... :) I even have Cygwin on Windows!..... ^^ I'm gonna try and come here more.... It's just that sometimes I just..... Withdraw.... :( I'm sorry, jas.... :( Ah, so the Terminal NCURSES at you if you put the Shebang in the wrong part of the Code.... :3 Oh, I just Love puns....... :3 Ok!..... ^^ Well, I have insults enabled in Bash, so I can take the heat!..... :3 BTW, when I first Coded Swiss, I listened to Dubstep meant for Hacking..... Not much Hacking there, though the Program COULD be used to destroy the victims HDD, if they run Windows, or DVL..... o_O But, we all know that DVL is MEANT to have SCSI Security measures..... :3 But the title of the Audio File says: "Dubstep Epic (Gamers - _Code is Art_", so I assume it goes with ANY kinda Programmin'..... :3 It sure FELT epic..... :D One day, I'm gonna make a Program that displays a crap-ton of GNU Pictures if the User is using Windows....... :3 ('Course, that would happen on my ASUS, obviously..... XD) Thanks for the help, good sir!!!..... ^^

No worries, glad to have helped!
 
You sure did!..... :3 BTW..... What Distro do you use.....? :) I don't think I ever asked..... :)

I've been using Kubuntu for a couple of years now, but I have used all of the major distros and a lot of lesser-known ones at various points over the years. I tend to favour Debian based distros, I find it quicker and easier to install and get my preferred working environment set up with Debian based distros. But I'm comfortable using any distro.

Although I'm currently running Kubuntu, I rarely use the KDE Plasma desktop. Instead I use dwm (a minimalistic tiling window manager) for most desktop sessions. The only time I tend to use a Plasma session is if I want to showcase GNU/Linux to friends/family/other interested parties. The rest of the time I use dwm which just lets me get on and work without any annoying distractions.
 

Members online


Top