I'm returning to college after a 21-year absence and am starting fresh in a science stream. My first course in January will be the introductory Python course. In the meantime, I'm doing the Codeacademy tutorials and have completed the basic ones on variables, strings, comments, conditionals, some functions, and a few other things. I also did a little project that's part of a tutorial section wrapup.

I took the time to write my first-ever independent script using the eric4 IDE in Linux Mint 14. I'm including the (very short) script in this post in case anyone has any kind, neutral or even nasty comments for me. I welcome all input.

#Prints welcome and asks for user input
print "Welcome to Miki's First Toy!"
print "This program tells you how long something you typed is."
variable = raw_input("Please type something in and hit enter: ")

#Determines if input is numeric and, if necessary, converts it to a string
if variable.isalpha() == False:
    print ""
    variable = str(variable)
    print ""

#Obtains length, provides it to user and exits
variable = str(len(variable))
print "What you typed is " + variable + " characters long."
print "Have a nice day!"



Recommended Answers

All 13 Replies

You can remove lines 6 to 11 because raw_input() always return a str object. The string's content is not examined, if the user types 123, the string "123" is returned. .isalpha() tests if all the characters in the string are alphabetic, for example "ps2pdf".isalpha() returns False.

Otherwise, variable is not a very good variable name, use something more descriptive like user_input or input_length.

Do you know if your january course uses python 2 or 3 ? If it is python 3, use python 3 right now !

Thanks so much for the quick and helpful reply. I wasn't aware that raw_input always returns a string because that was something I hadn't learned yet. I'll remove the redundant code and rename the variable. Do you have any comment on the fact that I use variable reassignment rather than new variable creation to restrict the script to one variable?

Can you recommend a Python 3 IDE for Linux? eric4 uses 2.71. Also, I'm saving my scripts as .py files for universality, but would it be a good idea to use some other file extension?

I don't use eric4, but it can probably be used to write python 3 code as well. On the other hand, you need to install python 3 to run your program. This post in a linuxmint forum could help you.

I read the post and used the instructions to check which version of python is my OS default, and it is indeed 3.2.3. It's my version of eric4 that can save python 3 files but not run them. I can't upgrade it through the built-in upgrader because it's bugged and not working, so I'll have to find another way--most likely by going to the application website and finding out what the latest version is, then seeing whether that version is in the repositories I use. If it's not, I'll just switch to a different IDE.

This is my revised script as per your input and my own decision to use unique variables for each discrete operation or function:

#Prints welcome and asks for user input
print "Welcome to Miki's First Toy!"
print "This program tells you how long something you typed is."
user_input = raw_input('Please type something in and hit enter: ')

#Obtains length, provides it to user and exits
string_length = str(len(user_input))
print "What you typed is " + string_length + " characters long."
print "Have a nice day!"

MUCH cleaner and still has what I, subjectively and in a biased way, think is a pretty good user interface for a non-graphical application. Once I learn to code graphical user interfaces, I'll create one for it, but first I want to think about making it just one in a bunch of linked modules under a groupware application intended to be an educational tool that helps small children learn counting, simple verbal skills and basic computer use. What do you think?

This is a simple version of your program..

def program():
    print("I am going to tell you how long a sentance you give me is!")
    sentance = raw_input("Give me a sentance \nSentance - ")
    sentance = len(sentance)
    print ("your sentance is", sentance, "characters long!")

Hi, Mat,

Much of the code you've removed or altered in your version is string literals that I included for the sake of enhancing the user interface. Given that string literals within a user interface heavily depend on language use, and that the word you proffer is actually spelled "sentence" rather than "sentance," I'm going to give your version of the user interface the appropriate amount of consideration and weight.

Thank you sincerely, however, for making me aware that there are certain technical points I didn't know about. Your algorithm is definitely cleaner than mine, so I'll revise my algorithm accordingly without disturbing the user interface any more than I can avoid.



Okay, I'm not allowed to edit that last post to make it better, so I'll just retract it here. Pretend I didn't say anything. Instead, I'll say this:

Computer end users don't see code; they see just the stuff displayed on their screen. Also, 99% of end users are lamers who don't read or know how to read code. Your code can be perfect, and you can be very smart and a first-rate programmer, but what an end user will see is "sentance," and that will make them not only stop using your program, but tell other people not to use ANYTHING with your name on it. I think that's important. I use only Linux, not Windows or a Mac OS, I support the GPL, and I strongly believe that technical merit should trump marketing considerations every time. But there's a difference between writing code as a sales tool (which we all hate, including the wretches working for Microsoft and Novell who HAVE to do it) and simply making what the end user sees clean and as approachable and enjoyable as you can make it without compromising code. I think I can accept Mat's help in making my code cleaner without having to eat the peel along with the orange by also using his string literals. If anybody disagrees, let me know.

I'm also wondering why your not using the newer verion of python? If your a newb you would be better off learning the latest version of python with simple changes like : input() instead of raw_input() and other little changes to make life easier for you.

I also apologise for my spelling mistake.. i didnt think it looked correct ;)


Hi, Mat,

I'm learning the old command sets that Codeacademy teaches and saving everything strictly as .py files. I can always upgrade my skills later to learn the newest command sets, but knowing both increases my versatility and availability. The people a GPL-release developer should be nicest to are the end users living on the poverty line who can afford only 15-year-old hardware, and that hardware requires old versions of OSs that support only obsolete Python interpreters. In commercial developer work, you really can't meet the needs of both those with outdated hardware and those with the latest hardware, but in Linux and GNU development work, it's not that difficult, and developing almost infinitely backwards-compatible applications is done routinely. So I hope I've cleared that up.

When I have a time window I'll revise my little module-like script as per your rewrite and post the latest revision here.

Python 2.7.3 has all the features of Python2 and also allows you to bring in features of Python3. Python3 has a lot of changes when it comes to byte strings that can bite (no pun intended) a beginner trying to use older code examples.

Okay, guys, after I complete my Codeacademy self-study, what resources can I consult to upgrade my skills from Python-obsolete to Python-3? If you can provide me with links to free-of-charge resources available online, that would be ideal, as my finances are limited and I'm a lot better at studying electronically than through paper.

I've bookmarked all of those pages, although the Google page didn't impress me by acting like it chewed up 1 gig of my swapfile. I'll be taking what Google has to say with a grain of salt because I'm kind of old-fashioned when it comes to bloat. In general, however, thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.