Check random numbers (randint)

Asked

Viewed 140 times

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

1 answer

1


The error that is happening is that you are asking if a string is larger or smaller than a number.

>>> rinfo = raw_input()
8
>>> type(rinfo)
<type 'str'>

As you can see the input returns a string, and the comparison ends up being made like this:

>>> rinfo = '1'
>>> rinfo > 20
True

If you do not define that it is an integer or float, it will not perform the comparison correctly.

The right one would be to transform the input that is string to integer:

>>> rinfo = int(raw_input())
8
>>> type(rinfo)
<type 'int'>
>>> rinfo > 3
True

Also, in the code there is no need to compare the same variable, this:

if rinfo == rinfo>rn:
    pass

That should be it:

if rinfo > rn:
    pass

Well, I redid the algorithm here because yours was always drawing the number again when the user made a new attempt. Your code could stay that way:

from random import *

def iniciar_jogo():

    numero_sorteado = randint(1, 99)
    print numero_sorteado, type(numero_sorteado)

    def pergunta():
        entrada = raw_input('Eu estou pensando em um numero de 1 a 99. \nConsegue adivinhar? ')
        if int(entrada) > numero_sorteado:
            print 'Menos do que isso'
            pergunta()
        elif int(entrada) < numero_sorteado:
            print "Mais do que isso"
            pergunta()
        else:
            print "Acertou! O numero era %i" % numero_sorteado
            decisao = raw_input('Deseja jogar novamente?')
            if decisao == 's':
                iniciar_jogo()
            else:
                print 'fim'

    # faz a primeira pergunta
    pergunta()

iniciar_jogo()

With the "drop out" option included:

from random import *

def iniciar_jogo():

    numero_sorteado = randint(1, 99)
    print numero_sorteado, type(numero_sorteado)

    def pergunta():
        entrada = raw_input('Eu estou pensando em um numero de 1 a 99. \nConsegue adivinhar? ')
        try:
            entrada = int(entrada)
        except ValueError:
            # se for do tipo string a entrada:
            if entrada == 'desisto':
                print "O numero era %i" % numero_sorteado
                jogar_novamente()
            else:
                print "Digite um numero para adivinhar ou 'desisto' para sair"
                pergunta()
        else:
            # se for do tipo inteiro:
            if entrada > numero_sorteado:
                print 'Menos do que isso'
                pergunta()
            elif entrada < numero_sorteado:
                print "Mais do que isso"
                pergunta()
            else:
                print "Acertou! O numero era %i" % numero_sorteado
                jogar_novamente()

    def jogar_novamente():
        decisao = raw_input('Deseja jogar novamente?')
        if decisao == 's':
            iniciar_jogo()
        else:
            print 'fim'

    pergunta()

iniciar_jogo()
  • 1

    Thank you very much fall to a tears >< I will try thank you very much thank you very much

  • 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 ?

  • as the same value can give 2 different answer ?

  • @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).

  • 1

    Orion it keeps giving two results for the same value what I do ?

  • @Fagner, his algorithm all the time makes a new attempt he draws a new number. I’ll give you an example in my answer.

  • @Fagner re-ran the algorithm, see how it turned out.

  • Thank you very much. I hope I didn’t disturb you, man... s2

  • @Fagner only thing missing is the "give up" option, I’ll include

  • @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.

  • 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 in Python is possible to make this kind of comparison, in this case (3 == 3 > 2) would return True, and (3 == 3 > 5) would return False, is the same as True if 3 > 2 > 1 else False.

  • 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".

  • @correct jsbueno, what is done is the transformation of a string for a integer.

  • 1

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

  • 1

    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

Show 11 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.