for 3-4 hours and im missing something that i cant find so far im still trying to do the if test for checking if the last letter of the humans guess matches the first letter of the computers word.
Ive got this so far:
for line in open('animals.txt'):
animals.append(line.strip().lower())
human = raw_input("Human: ")
last = human[-1]
if last == guess[-1]:
guess = random.choice(animals)
print "computer: " + guess.lower()
else:
print "you lose"
guess = random.choice(animals)
print "computer: " + guess.lower()
it works properly (does the if, else when its supposed to) only if I enter one letter for the humans guess if I enter a word it just does the else
except for the word eagle for some reason..... that does the if when its supposed to
Any help is appreciated _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
Thu Aug 07, 2008 10:40 pm
Spockie-Tech Site Admin
Joined: 31 May 2004
Posts: 3160
Location: Melbourne, Australia
That looks way weird to me !
I'm still learning Python myself, so I might have some structural stuff wrong..
but it looks to me like you are making your random.choice from the animals list *before* you have even read the list in from the animals.txt file, which means you are probably selecting from an empty list
Why would you want to check if the last human letter equals the first animal letter ?
You need to indent your code to show the structure a bit better
More help than that requires the animals.txt file, which isnt easily downloaded from that site without registering. _________________ Great minds discuss ideas. Average minds discuss events. Small minds discuss people
Fri Aug 08, 2008 12:21 am
cerberus3112
Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW
for line in open('animals.txt'):
animals.append(line.strip().lower())
human = raw_input("Human: ")
last = human[-1]
if last == guess[-1]:
guess = random.choice(animals)
print "computer: " + guess.lower()
else:
print "you lose"
guess = random.choice(animals)
print "computer: " + guess.lower()
The animals .txt is a notepad that has alot of animals the program picks a animal and displays it like
computer: (its guess) then goes
human: (my guess) the problem is a test goes
computer: ferret (this is a random animal from the list)
human: tiger
you lose!
computer:ferret (this is a random animal from the list)
human:t
it guesses again so on it should accept a word begining with the same letter as the computers last guess. _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
Fri Aug 08, 2008 8:08 am
Valen Experienced Roboteer
Joined: 07 Jul 2004
Posts: 4436
Location: Sydney
you made a mistake with the letters your comparing,
your comparing the last letter the human enters vs the last in the computers guess
meant to be the first the human has.
I sort of did it for you lol.
code:
import random
animals = [] #holds the list of animals
for line in open('animals.txt'):
animals.append(line.strip().lower()) #read in the list of animals strip off white space and convert it to lower case
winning = True
round = 0
while winning: #keep playing while the user is winning
round += 1
guess = random.choice(animals) #pick an animal, any animal
print "Round %s : computer guesses : %s " % (round,guess)
human = raw_input("Human: ") #Get the users guess
last = human[0].lower() #convert it to lower case incase they capatilse the name of the animal
if last == guess[-1]:
print "You won, well done"
else:
print "you lost after %s rounds" % (round)
winning = False #stop looping, cos the player is a l00ser, not worthy of game
just for fun the computer now doesnt cheat
use the "code" tag to preserve your formatting
code:
import random
animals = [] #holds the list of animals
for line in open('animals.txt'):
animals.append(line.strip().lower()) #read in the list of animals strip off white space and convert it to lower case
winning = True
round = 0
firstletter = random.choice("abcdefghijklmnopqrstuvwxyz") #import string, then put string.lowercase here for more correctness and portablility
while winning: #keep playing while the user is winning
round += 1
guess = random.choice([elem for elem in animals if elem[0] == firstletter]) #pick an animal, from a list that starts with the users guess
human = raw_input("Human: ") #Get the users guess
last = human[0].lower() #convert it to lower case incase they capatilse the name of the animal
if last == guess[-1]:
if human.lower() not in animals: #check the list for their guess
print "I'll take your word for it ;->"
print "You won, well done"
firstletter = human[-1]
else:
print "you lost after %s rounds" % (round)
winning = False #stop looping, cos the player is a l00ser, not worthy of game
One programming practise you should really take on board from Jakes examples is to comment everything! Adding funny quotes like "#stop looping, cos the player is a l00ser, not worthy of game" makes reading code that little bit less serious to anyone else _________________ https://www.halfdonethings.com/
Fri Aug 08, 2008 1:12 pm
dyrodium Experienced Roboteer
Joined: 24 Aug 2004
Posts: 6476
Location: Sydney
Rofl nice... awesome that you have the chance to do stuff like this in school, mine was total crap house and even told me never to bring my robots to school... bastards!? _________________ ( •_•)
Oh yeah i forgot
The list of animals doesnt have one for every letter
so if you run that it can die (the one where it plays fair)
I added these to the animals list, you could try just appending them to the list after its read if they want to run it on their own animals.txt file
Ibex
Unicorn
Vulture
Xenops
(okay unicorn is cheating, for extra extra credit you could let the computer loose ;->) _________________ Mechanical engineers build weapons, civil engineers build targets
Fri Aug 08, 2008 3:08 pm
cerberus3112
Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW
thanks for the help and the tips
@angus
yeah i wouldnt have know bout this except i got a new multimedia teacher and he told me about it and said that i should do it.
You can register on the website if you are attending a school i think the condtion was the school donst have to register for you also Im not sure if the school will allow me to bring robots to school on the fact that mines still not finished yet AND it not weaponded.... _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW
0_0 thats not funny
The program worked perfectly and did everything it was meant to but the reason it didn't get accepted was because it didn't end after it printed you lose
which was shown in the picture but not talked about in the text _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
Fri Aug 08, 2008 7:01 pm
cerberus3112
Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW
Question 5. basically fix the code for teh animal game so that is dosn't cheat and loses . _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
Mon Aug 18, 2008 8:34 pm
cerberus3112
Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW
this ones REALLY confusing
I need to find out how to define the variables in the "<"(variable)">" structure of rules.txt
then how to check for them and update there values
im close but thats the part im missing _________________ A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum