Sunday, 1 February 2009

My first computer program: Humans versus Computers


An essential part of modern evolution and population biology is knowing how to program. By being able to write scripts one is able to analyze data in a way of ones choosing as opposed to having to rely on someone else's program. This can also be used to quickly manipulate sequence files without having to go in and change them manually. Next week I'm going to learn how to use a powerful database program called BIONUMERICS, for this I need to understand the programming language Python. I have been teaching myself it over the last few weeks and i wanted to publish the first real program i wrote myself. This program is just a bit of fun really, basically you are asked to pick a number between 1 and 100 and then the computer will try and guess, in the fewest attempts what that number is. once the computer has guessed then you will have to guess the computers number. Whomever guessed it in the least attempts wins.
Python is a really powerful language and can be obtained from (http://www.python.org), I would advise learning how to use python as not only is it useful but quite fun too. There is lots of support for biologist and some specific verions of python with useful modules in like scipy (http:// www. scipy.org) and biopython (http:// www. biopython.org).

print "\t\t\tWelcome to \"Guess My Number\"!"
print "\nThink of a number between 1 and 100."
print "I will try to guess it in as few attempts as possible.\n"

import random

the_number = input ("Enter the number: ")

#the computer guesses the number using the random function

guess = random.randrange(100)+1
tries = 1

while (guess != the_number):
if (guess > the_number):
print "I chose", guess, "Wrong!!! The number is Lower ..."
guess = random.randint(1, guess-1)
else:
print "I chose", guess, "Wrong!!! The number is Higher ..."
#setting limits to guesses
guess = random.randint(guess+1, 100)
tries += 1

print "I guessed it! The number was",the_number
print "I got it in ", tries, "tries!\n"

#set intial values
computer_number = random.randrange(100)+1
human_guess=int(raw_input("take a guess"))
human_tries=1

#human_guess = loop

while (human_guess != computer_number):
if (human_guess>computer_number):
print "lower..."
else:
print"higher..."
human_guess = int(raw_input("take a guess:"))
human_tries += 1
print "you guessed it! The number was",computer_number
print "you got it in ", human_tries, "tries!\n"

if (human_tries>tries):
print "computers win"
else:
print"humans win"

raw_input ("To end press .")

6 comments:

Christie Lynn said...

Very cool!

PS tagged you in a Science Books meme.

Dolf said...

Note that no prior knowledge of Python is required to use the BioNumerics software. However, BioNumerics does offer the opportunity to use [Python] scripts to customize data import/export, analysis and reporting.

sager said...

Congratulation for the great post. Those who come to read your article will find lots of helpful and informative tips.

Acer - 15.6" TravelMate Notebook - 4 GB Memory - 750 GB Hard Drive

Acer - 15.6" Aspire Notebook - 6 GB Memory - 640 GB Hard Drive - Black

Alamin Miah said...

Hurrah, that’s what I was searching for, what a material! present here at this weblog, thanks admin of this web site.| https://adamfantacy.tumblr.com/

Alamin Miah said...

After going over a few of the articles on your blog, I truly appreciate your way of writing a blog. I bookmarked it to my bookmark website list and will be checking back in the near future. Please visit my web site as well and tell me your opinion. My Blog http://megaworld.beep.com/

Mona afrin said...

Why do you need New York Escort? You have been working so hard day and night,you deserve to have time to relax your body,After this,You must wanna meet one hot girl in street or in the nightclub and have a perfect encounter.When you want it come true so hardly,it will come true.New York Escort would be the best choice for you guys.Just image it,Hot New York Escort walk through the street or drink in the bar with you,You can see the jealous face from people.Every New York Escort have the Devil’s body and Angle’s face. My Blog http://flickreel.page.tl