|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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????? |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
||||
|
||||
|
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. |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > problem with this code |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|