Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Closed Thread
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 14th, 2004, 12:41 AM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
problem with this code

sory i do this but i need to do the book axcersices and i dont have the answers...
tha bad of this code is that the user interface part its in spanish, sory for that
the point of the game is that a random word will be chosen from a tuple then it will be written with the letters in different places and the user have to guess... you can ask for a clue but than reduces your score by 25%
well i hope you understand the program if not i can try to translate
bye thanks very much

Code:
# Adivina Adivinador 1.0.0 build 15
#
# Proposito del juego: Adivinar la palabra desordenada,
# El usuario puede pedir pistas, peor eso rest apuntos.
#
# Por IvanHope - 13 NOV 2004

import random

score = 0

print """
Bienvenido a "Adivina Adivinador 1.0" Por IvanHope
El programa le dara una palabra desordenada y hay que adivinarla.
el usuario tiene opcion a ver una pista pero eso bajael puntaje.
Tienes 10 oportunidades por adivinanza.

A JUGAR!!!!!!!!!!!
"""

nombre = raw_input("Escribe tu nombre: ")
raw_input(nombre, "Presiona ENTER para comenzar")

while seguir != "no":
  seguir = "si"
  clue = ""
  word = ""
  used = ""
  adivina = ""
  intento = 0
  elements = ("heterogeneo", "argentina", "aleluya", "elefante", "importante", "guillotina", "esmeralda", "millonario")
  word = random.choice(elements)
  correct = word
  
  if word = elements[1]:
    clue = "homogeneo"
  elif word = elements[2]:
    clue = "patagonia"
  elif word = elements[3]:
    clue = "amen"
  elif word = elements[4]:
    clue = "marfil"
  elif word = elements[5]:
    clue = "indispensable"
  elif word = elements[6]:
    clue = "revolucion francesa"
  elif word = elements[7]:
    clue = "verde"
  elif word = elements[8]:
    clue = "$$$$$$"
  
  while word:
    temp = int(len(word))-1
    temp = random.randrange(temp)
    adivina += word[temp]
    word = word[:temp] + word[temp-1:]
  
  while guess != correct:
    print "Tu palabra desordenada es: [", adivina, "]"
    guess = raw_input("Intento N: ", intento, ", escribe la palabra correctamente o escribe 'pista' para recibir la misma: ")
    guess = guess.lower()
    if guess = "pista":
      print clue
      used = "yes"
    intento += 1
  
  score = 100.00/intento
  if used = "yes":
    score = score*0.75
  print "Por Fin Acertaste!!!! - Fueron necesarios: ", intento, " intentos."
  print "Tu puntaje actual es: ", score, " puntos."
  if used = "yes":
    print "Se te ha restado un 25% de tu puntaje por usar pista en esta palabra"
  seguir = raw_input("desea continuar?")
  seguir = seguir.lower()

Last edited by Scorpions4ever : August 3rd, 2007 at 05:36 PM.

Reply With Quote
  #2  
Old November 14th, 2004, 12:43 PM
sfb sfb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 447 sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level)sfb User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 1 h 43 m 45 sec
Reputation Power: 10
Lines like these...

Code:
if word = elements[1]:
...
if guess = "pista":
...
if used = "yes":


are the cause of some syntax errors - to do a comparison test (if X equals Y) Python needs two equals signs, like this:

Code:
if word == elements[1]:
...
if guess == "pista":
...
if used == "yes":



Also generally, this code:
Code:
  elements = ("heterogeneo", "argentina", "aleluya", "elefante", "importante", "guillotina", "esmeralda", "millonario")
  word = random.choice(elements)
  correct = word
  
  if word = elements[1]:
    clue = "homogeneo"
  elif word = elements[2]:
    clue = "patagonia"
  elif word = elements[3]:
    clue = "amen"
  elif word = elements[4]:
    clue = "marfil"
  elif word = elements[5]:
    clue = "indispensable"
  elif word = elements[6]:
    clue = "revolucion francesa"
  elif word = elements[7]:
    clue = "verde"
  elif word = elements[8]:
    clue = "$$$$$$"


could be changed into something a bit shorter and tidier (and more extensible), e.g.:

Code:
  elements = (
    ("heterogeneo", "homogeneo"), ("argentina", "patagonia"), ("aleluya", "amen"), 
    ("elefante", "marfil"), ("importante", "indispensable"), 
    ("guillotina", "revolucion francesa"), ("esmeralda", "verde"), ("millonario", "$$$$$$")
  )
  
  word, clue = random.choice(elements)
  correct = word 

Reply With Quote
  #3  
Old November 14th, 2004, 02:48 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
thanks man you are a great advisor, hehe
thanks very much

Code:
  elements = (
    ("heterogeneo", "homogeneo"), ("argentina", "patagonia"), ("aleluya", "amen"), 
    ("elefante", "marfil"), ("importante", "indispensable"), 
    ("guillotina", "revolucion francesa"), ("esmeralda", "verde"), ("millonario", "$$$$$$")
  )
  
  word, clue = random.choice(elements)
  correct = word 


in this case you show me: tthe random.choice will choose one "()" from the tuple and when i use word, clue i am telling the program like first and second element of the sub-tuple?????

Reply With Quote
  #4  
Old November 14th, 2004, 03:02 PM
ivanhope ivanhope is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 35 ivanhope User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 59 m 4 sec
Reputation Power: 5
this is what i get now:

Code:
 
D:\Trabajos\Python>python  adivinador.py

Bienvenido a "Adivina Adivinador 1.0" Por IvanHope
El programa le dara una palabra desordenada y hay que adivinarla.
el usuario tiene opcion a ver una pista pero eso bajael puntaje.
Tienes 10 oportunidades por adivinanza.

A JUGAR!!!!!!!!!!!

Escribe tu nombre: ivan
Traceback (most recent call last):
  File "adivinador.py", line 43, in ?
    temp = random.randrange(temp)
  File "C:\Program Files\python\lib\random.py", line 154, in randrange
    raise ValueError, "empty range for randrange()"
ValueError: empty range for randrange()




line 43 is this:
Code:
  while word:
    temp = int(len(word))-1
 43 temp = random.randrange(temp)
    adivina += word[temp]
    word = word[:temp] + word[temp-1:]

Last edited by Scorpions4ever : June 18th, 2008 at 01:33 PM.

Reply With Quote
  #5  
Old November 14th, 2004, 08:13 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 11 m 13 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
At some point in your loop, temp becomes 0. In which case this happens:

Code:
>>> import random
>>> random.randrange(0)

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in -toplevel-
    random.randrange(0)
  File "C:\Python23\lib\random.py", line 154, in randrange
    raise ValueError, "empty range for randrange()"
ValueError: empty range for randrange()
>>> 


As a rule though, I would use randint() over randrange(). If only because it avoids this error . Alternatively you could test temp to see if it is 0 then break from the loop.

http://www.python.org/doc/2.3.4/lib/module-random.html

Mark.
__________________
programming language development: www.netytan.com Hula


Reply With Quote
Closed Thread

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > problem with this code


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT