Programs for different language learning - Python I've created a program for my own purposes in three different languages, Perl, Python, and Ruby. Each program will be posted in with their own tag but I will add links to them through on each post. The program itself is very simple, all it does is ask the user about three different languages which are saved into an array, it then prompts the user to choose a language that runs through an if statement, and if the user chooses the language they're writing in it then takes a random language from the array and outputs it.

See also Perl

See also Ruby

Python:

import random

def welcome():
    choices = ["Perl", "Python", "Ruby"]
    lang = 3
    print "Welcome, to the test script, this will test what language you would like to learn.. In order to find out these choices, write this same definition in all three different languages\n"
    print "There are", lang, "languages to choose from please pick one\n"
    user_choice = raw_input(choices)
    if user_choice == "Ruby":
        print("You have chosen Ruby!\n")
    elif user_choice == "Perl":
        print("You have chosen Perl!\n")
    else: 
        print("You're already writing in Python!! Let me choose for you! ")
        print random.choice(["Perl", "Ruby"])
welcome()


Creating this program in Python was actually fairly easy, it was my second program I've ever written in Python and reminds me a lot of Ruby. However I kept getting an error while running it due to the fact that I didn't import random, after figuring this out I found out that I had to write out each language's name instead of making it random from the array.. Also Python ended up being the shortest maxing out at 22 lines, I will be adding the execution time of the program shortly.

I also have some questions about Python:


In Python is there a equivalent to Ruby's puts?
In the function print random.choice(["Perl", "Ruby"]) is there a way I can pull from the array instead of having to write out language names
You should use len rather than hard-code the length of choices.
You should use str.format to add lang into the string. 'there are {} languages'.format(lang).
You mix print '' and print(''). Pick one or the other. The former is preferred in Python2, the latter in Python3.
Reduce the amount of prints. They are slow, and merging them into one is much faster.
You should use raw_input().lower(), so that the user can enter 'rUBy'. Where it converts it to lower case, 'ruby'.
Use elif not else. If I enter C, I don't want to be told I entered Python.



  In Python is there a equivalent to Ruby's puts?


In Python2 and 3 you can use '\n'.join(list).
In Python3 you can use print(*list, sep='\n').


  In the function print random.choice(["Perl", "Ruby"]) is there a way I can pull from the array instead of having to write out language names


You may mean filter the list, so it does not include 'Python'.

random.choice(filter(lambda lang: lang != "Python", choices))




import random

def welcome():
    choices = ["Perl", "Python", "Ruby"]
    user_choice = raw_input(
        (
            "Welcome, to the test script, this will test what language you "
            "would like to learn.. In order to find out these choices, write "
            "this same definition in all three different languages\n\n"
            "There are {} languages to choose from please pick one\n"
            "{}"
        ).format(len(choices), choices)
    ).lower()

    if user_choice == "ruby":
        print "You have chosen Ruby!\n"
    elif user_choice == "perl":
        print "You have chosen Perl!\n"
    else user_choice == "python":
        print "You're already writing in Python!!Let me choose for you! \n{}".format(
            random.choice(["Perl", "Ruby"]))
    else:
        print "Invalid choice."

welcome()