How can I change the value of the variables every time I repeat the function def?

-3

My algorithm solves the following problem: Create an algorithm that reads 4 values typed and indicates if any pair of numbers adds 8 or a number greater than 8.

    s = [int(input()) for c in range(4)]
while sum(s) < 8:
    print('Error 01: Erro: A soma de todos os 4 números resulta num número menor que 8, digite outros números.')
    s = [int(input()) for c in range(4)]
    if sum(s) > 8:
        break
x = 0
y = 0
z = 1
s = sum(s[y] + s[z])
while x < 6 or s < 8:
    def funcao(x = 0, y = 0, z = 1):
        if s == 8:
            print(f'Os números {s[y]} e  {s[z]} somados dão 8. ')
        elif s > 8:
            print(f'Os números {s[y]} e  {s[z]} somados resultam num número maior que 8. ')
        else:
            if x == 3 and s < 8:
                funcao(x+1,y+1,z+1)
    
asked by anonymous 14.12.2017 / 17:05

1 answer

3
 def funcao (x=0, y=0, z=1):

    while x < 3:
        s = sum(s[y] + s[z])
        z += 1
        x += 1
        if s == 8:
            print(f'Os números {s[y]} e  {s[z]} somados dão 8. ')
        elif s > 8:
            print(f'Os números {s[y]} e  {s[z]} somados resultam num número maior que 8. ')
        elif x == 3 and s < 8:
            funcao(x+1,y+1,z+1)

This is a confusing way to understand what you want because I feel the code is incomplete, after all, I'm not seeing it.

When you start the def function if you do not declare values it will start at x = 0, y = 0 and z = 1, same as you put at the beginning of the function. And at the end of it, it will get the values it already has x, y, z and put +1 on them.

    
16.12.2017 / 12:15