Post Reply 
What Was Your First Programming Language?
07-09-2015, 02:00 AM (This post was last modified: 07-09-2015 07:17 AM by Les Bell.)
Post: #89
RE: What Was Your First Programming Language?
(07-09-2015 01:55 AM)Sylvain Cote Wrote:  
Code:
print "Too high!" if ($guess > $number);
print "Too low!" if ($guess < $number);

Hey, cool, Sylvain - thanks!! I'd forgotten that particular Perl paradigm - I've been away from it for too long, I guess. I'll change my version here, because that neatly demonstrates the Perl mantra: TMTOWTDI (There's More Than One Way To Do It)!

And because I'm in the mood, here's a version for Python 3.x, written in a very plain-vanilla style:

Code:

# guess.py (for Python 3)
import random
name = input("Hi there, what's your name? ")
play = input("OK, " + name + ", would you like to play a game? ")
while play.upper() == 'Y' :
    number = int(random.random() * 100) + 1
    print("I'm thinking of a number between 1 and 100.")
    print("You've got to try to guess it.")
    guess = int(input("What's your guess? "))
    while guess != number :
        if guess > number :
            print("Too high! ")
        if guess < number :
            print("Too low! ")
        guess = int(input("What's your next guess? "))
    print("You got it, " + name + "!!!\n")
    play = input("Play again? ")

--- Les
[http://www.lesbell.com.au]
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: What Was Your First Programming Language? - Les Bell - 07-09-2015 02:00 AM



User(s) browsing this thread: 2 Guest(s)