www.robowars.org

RoboWars Australia Forum Index -> Off-Topic

python help
Goto page 1, 2  Next

Post new topic   Reply to topic
  Author    Thread
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  
python help

I'm doing a python coding challenge for school/fun/learning and Ive been working at this question
http://challenge.ncss.edu.au/files/31db2bd0/questions2.pdf Question 5 the animals one.

for 3-4 hours Rolling Eyes 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:

import random
animals = []
guess = random.choice(animals)
print "computer: " + guess.lower()

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..... Confused that does the if when its supposed to
Any help is appreciated Very Happy
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Thu Aug 07, 2008 10:40 pm 
 View user's profile Send private message Send e-mail
Spockie-Tech
Site Admin


Joined: 31 May 2004
Posts: 3160
Location: Melbourne, Australia


 Reply with quote  

That looks way weird to me ! Shocked

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

For a quick ref to Python (that links to a better ref), look here
http://www.yukoncollege.yk.ca/~ttopper/COMP118/rCheatSheet.html

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

Post Fri Aug 08, 2008 12:21 am 
 View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

I indented the code but the


quote:

import random
animals = []
guess = random.choice(animals)
print "computer: " + guess.lower()

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

Post Fri Aug 08, 2008 8:08 am 
 View user's profile Send private message Send e-mail
Valen
Experienced Roboteer


Joined: 07 Jul 2004
Posts: 4436
Location: Sydney


 Reply with quote  

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



_________________
Mechanical engineers build weapons, civil engineers build targets

Post Fri Aug 08, 2008 11:19 am 
 View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Valen
Experienced Roboteer


Joined: 07 Jul 2004
Posts: 4436
Location: Sydney


 Reply with quote  

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

  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]:
    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



_________________
Mechanical engineers build weapons, civil engineers build targets

Post Fri Aug 08, 2008 12:01 pm 
 View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
Knightrous
Site Admin


Joined: 15 Jun 2004
Posts: 8511
Location: NSW


 Reply with quote  

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 Wink
_________________
https://www.halfdonethings.com/

Post Fri Aug 08, 2008 1:12 pm 
 View user's profile Send private message
dyrodium
Experienced Roboteer


Joined: 24 Aug 2004
Posts: 6476
Location: Sydney


 Reply with quote  

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!? Shocked
_________________
( •_•)

( •_•)>⌐■-■

(⌐■_■)

YEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH

Post Fri Aug 08, 2008 1:58 pm 
 View user's profile Send private message Visit poster's website MSN Messenger
Valen
Experienced Roboteer


Joined: 07 Jul 2004
Posts: 4436
Location: Sydney


 Reply with quote  

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

Post Fri Aug 08, 2008 3:08 pm 
 View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

thanks for the help and the tips Very Happy

@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 Razz 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.... Rolling Eyes
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Fri Aug 08, 2008 5:14 pm 
 View user's profile Send private message Send e-mail
Knightrous
Site Admin


Joined: 15 Jun 2004
Posts: 8511
Location: NSW


 Reply with quote  

I'm still at school.... Cool sorta Wink
_________________
https://www.halfdonethings.com/

Post Fri Aug 08, 2008 5:29 pm 
 View user's profile Send private message
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

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 Evil or Very Mad
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Fri Aug 08, 2008 7:01 pm 
 View user's profile Send private message Send e-mail
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

thanks valen you already did this weeks answer Very Happy

http://challenge.ncss.edu.au/challenge/submit/4/5/
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Mon Aug 18, 2008 7:47 pm 
 View user's profile Send private message Send e-mail
Valen
Experienced Roboteer


Joined: 07 Jul 2004
Posts: 4436
Location: Sydney


 Reply with quote  

thats a login link?
_________________
Mechanical engineers build weapons, civil engineers build targets

Post Mon Aug 18, 2008 8:02 pm 
 View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

its the link to the question but you have to be logged in to see it so new linky

http://challenge.ncss.edu.au/files/105c4ed9/questions4.pdf

Question 5. basically fix the code for teh animal game so that is dosn't cheat and loses Very Happy.
_________________
A journey of a million miles begins with a single step followed by a hell of a lot of other steps so get walking

Post Mon Aug 18, 2008 8:34 pm 
 View user's profile Send private message Send e-mail
cerberus3112



Joined: 05 Dec 2005
Posts: 497
Location: Mt Druitt,Sydney,NSW


 Reply with quote  

okay i need some help again

http://challenge.ncss.edu.au/files/31db2bd7/questions5.pdf

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

Post Sat Aug 30, 2008 9:12 pm 
 View user's profile Send private message Send e-mail
  Display posts from previous:      

Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 2

Goto page 1, 2  Next

Forum Rules:
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

 

Last Thread | Next Thread  >
Powered by phpBB: © 2001 phpBB Group
millenniumFalcon Template By Vereor.