1
I started learning python this week and I was asking some questions and in one I have several different answers more when I type them always returns the same thing as if the others did not exist
This and the code:
from random import*
espaco = " "
Question_4 = "Eu estou pensando em um numero de 1 a 99. \nConsegue adivinhar ?"
Valor_maior = "Menos que isso"
Valor_menor = "Mais que isso"
def pergunta4():
number = randint(1,99)
print espaco
print Question_4
print espaco
resposta = raw_input ()
if resposta == resposta > number:
print espaco
print Valor_menor
print espaco
print pergunta4()
elif resposta == resposta < number:
print espaco
print Valor_maior
print espaco
print pergunta4()
elif resposta == number:
print espaco
print "E isso ae"
print espaco
print replay()
elif resposta == "desisto":
print espaco
print "ohh que pena o resultado era "
print number
print espaco
print replay()
def replay():
print espaco
print "Deseja brincar novamente ? (s/n)"
print espaco
decisao = raw_input ()
if decisao == "s":
print pergunta4()
if decisao == "n":
print "ok"
pergunta4()
run to see if this good anything else I type returns me the same thing could help me ?
the main idea was that it would generate any number between 1 to 99 and if I said a greater number than he did he would answer me that the number is smaller and if I spoke a smaller number he would say it was bigger until I hit the error more and basically this
Eu estou pensando em um numero de 1 a 99.
Consegue adivinhar ?
99999
Mais que isso
I said the number was 99999 and he said it was more than that plus the limit was 99 :( and no matter the value always appears that yes I modified the code to help read
Thank you very much fall to a tears >< I will try thank you very much thank you very much
– Fagner
now you’re giving it I’m thinking of a number from 1 to 99. Can you guess ? 63 Less than that I’m thinking of a number from 1 to 99. Can you guess ? 63 More than this I’m thinking of a number from 1 to 99. Can you guess ?
– Fagner
as the same value can give 2 different answer ?
– Fagner
@Fagner if the input is both number and text, you can use
rinfo = raw_input()
and at the time of comparison treat type. ex(if int(rinfo) > rn
).– Paulo
Orion it keeps giving two results for the same value what I do ?
– Fagner
@Fagner, his algorithm all the time makes a new attempt he draws a new number. I’ll give you an example in my answer.
– Paulo
@Fagner re-ran the algorithm, see how it turned out.
– Paulo
Thank you very much. I hope I didn’t disturb you, man... s2
– Fagner
@Fagner only thing missing is the "give up" option, I’ll include
– Paulo
@Fagner added the 'give up' option. Using exception the code tries to convert the input to integer, if an error occurs, it is checked if the user wanted to give up, if it was a number, the verification is performed.
– Paulo
When you say that "there is no need to compare the same variable", it gives the impression that the
if rinfo == rinfo>rn
would work. Is that right? I think not, right? Even if the precedence of the==
was bigger (I don’t think it is), the left side would end up giving true, spoiling the comparison with the>
.– bfavaretto
@bfavaretto in Python is possible to make this kind of comparison, in this case
(3 == 3 > 2)
would returnTrue
, and(3 == 3 > 5)
would returnFalse
, is the same asTrue if 3 > 2 > 1 else False
.– Paulo
Please note that "It would be right to inform that it is an integer" is an incorrect statement.
int(x)
generates a new integer from the object "x" What is snedo ai is a creation of an inteito from a string - you can even think of it as a "conversion" - but not "inform that it is".– jsbueno
@correct jsbueno, what is done is the transformation of a
string
for ainteger
.– Paulo
I tested it and it really works. I just don’t quite understand how, I’ll study more python. Or who knows how to post a question on the subject here on the site :)
– bfavaretto
I posted a question: http://answall.com/questions/68249/como-o-python-interpreta-v%C3%A1rios-operadores-de-compara%C3%A7%C3%A3o-em-sequ%C3%Aancia
– bfavaretto