1
\$\begingroup\$

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
\$\endgroup\$

1 Answer 1

4
\$\begingroup\$
  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()
\$\endgroup\$
7
  • \$\begingroup\$ So raw_input().lower() will make it so that they can type it in, in anyway, and get the same result? example PytHoN, pYTHON \$\endgroup\$
    – 13aal
    Commented Nov 10, 2015 at 13:53
  • \$\begingroup\$ 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 \$\endgroup\$
    – 13aal
    Commented Nov 10, 2015 at 13:54
  • 1
    \$\begingroup\$ @LostBam raw_input().lower() converts input to lowercase. Edited answer. \$\endgroup\$
    – Peilonrayz
    Commented Nov 10, 2015 at 13:57
  • 2
    \$\begingroup\$ @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... \$\endgroup\$
    – Peilonrayz
    Commented Nov 10, 2015 at 15:44
  • \$\begingroup\$ One more question, does Python have a function to use white space for arrays? Like Ruby's w%(#array item) \$\endgroup\$
    – 13aal
    Commented Nov 10, 2015 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.