Check random numbers (randint)

1

I started learning python this week and was asking a few questions and in one has several different answers when the digits always return the same thing to me as if the others did not exist

This is 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()

I run to see if this good plus anything I type returns the same thing could help me?

The main idea was that it generated any number between 1 and 99 and if I said a larger number of what he did he would answer that the number is smaller and if I said a smaller number he would say it was bigger until I to fix 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 what the value always appears this  yes I modified the code to help read

    
asked by anonymous 09.06.2015 / 04:34

1 answer

1

The error that is occurring 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 entry returns a string, and the comparison ends up being done like this:

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

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

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

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

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

if rinfo == rinfo>rn:
    pass

It should be this:

if rinfo > rn:
    pass

Well, I've refacted the algorithm here since yours was always drawing the number again when the user made a new attempt. Your code could look like this:

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 "I give up" 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()
    
09.06.2015 / 05:13