# 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.

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

1. You should use len rather than hard-code the length of choices.
2. You should use str.format to add lang into the string. 'there are {} languages'.format(lang).
3. You mix print '' and print(''). Pick one or the other. The former is preferred in Python2, the latter in Python3.
4. Reduce the amount of prints. They are slow, and merging them into one is much faster.
5. You should use raw_input().lower(), so that the user can enter 'rUBy'. Where it converts it to lower case, 'ruby'.
6. 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()

• So raw_input().lower() will make it so that they can type it in, in anyway, and get the same result? example PytHoN, pYTHON – 13aal Nov 10 '15 at 13:53
• In the function print random.choice(["Perl", "Ruby"]) is there a way I can pull from the array instead of having to write out Edited the question – 13aal Nov 10 '15 at 13:54
• @LostBam raw_input().lower() converts input to lowercase. Edited answer. – Peilonrayz Nov 10 '15 at 13:57
• @LostBam no problem! Also don't forget 'wait between asking similar questions'. People were annoyed by your three similar questions. Making a forth some time soon would be a bad idea... – Peilonrayz Nov 10 '15 at 15:44
• One more question, does Python have a function to use white space for arrays? Like Ruby's w%(#array item) – 13aal Nov 10 '15 at 15:47