how do I get these if statement to work?

1

What happens is that when I run my program, the output repeats the questions more than usual (sometimes 4 times sometimes 2 or 1).

My mistake may be because I call the functions many times or because I misuse the if statement but I'm not sure. Can you get me out of doubt? Thanks in advance.

def respuesta_pc():
  compu_respuesta = input('esta es la respuesta de la computadora:')
  return compu_respuesta

def respuesta_humana():
  huma_respuesta = input('esta es tu respuesta')
  return huma_respuesta

if  respuesta_pc()== 'roca' and  respuesta_humana() == 'papel' :
  print('el papel le gana a la roca.tu ganas')
elif respuesta_pc() == 'roca' and  respuesta_humana() == 'tijera' :
  print('la roca le gana a la tijera. tu pierdes')
elif respuesta_pc() == 'roca' and  respuesta_humana() == 'roca' :
  print('es empate')
else:
  print('tu respuesta no es valida')

The program is just beginning, so I'm testing the answers with rock.

Answer by pc = rock

Your answer = (the one you want)

    
asked by juan 13.10.2018 в 01:29
source

1 answer

1

You are calling the functions every time you go through a if and that's why they ask for more account information. If it is not necessary, calls to the functions are made once and the returned value is assigned to a variable.

On the other hand, more options are missing in the comparison:

roca - papel
roca - tijera
papel - roca
papel - tijera
tijera - roca
tijera - papel
- cuando son iguales -

With that in mind, the code may look something like this:

def respuesta_pc():
    compu_respuesta = input('Esta es la respuesta de la computadora: ')
    return compu_respuesta

def respuesta_humana():
    huma_respuesta = input('Esta es tu respuesta: ')
    return huma_respuesta

resp_pc = respuesta_pc()
resp_hm = respuesta_humana()
opciones = ['roca', 'papel', 'tijera']

if resp_pc not in opciones:
    print('La respuesta de la computadora no es valida')
elif resp_hm not in opciones:
    print('Tu respuesta no es valida')
elif resp_pc == 'roca' and resp_hm == 'papel' :
    print('El papel le gana a la roca. Tu ganas')
elif resp_pc == 'roca' and resp_hm == 'tijera' :
    print('La roca le gana a la tijera. Tu pierdes')
elif resp_pc == 'papel' and resp_hm == 'roca' :
    print('El papel le gana a la roca. Tu pierdes')
elif resp_pc == 'papel' and resp_hm == 'tijera' :
    print('La tijera le gana al papel. Tu Ganas')
elif resp_pc == 'tijera' and resp_hm == 'roca' :
    print('La roca le gana a la tijera. Tu Ganas')
elif resp_pc == 'tijera' and resp_hm == 'papel' :
    print('La tijera le gana al papel. Tu pierdes')
else:
    print('Es empate')
    
answered by 13.10.2018 / 03:00
source