Python - How to calculate the predecessor of a number?

Asked

Viewed 64 times

-4

Create a function called predecessor that takes as a single parameter an integer a and prints its predecessor.

Entree The input consists of a single row containing the integer number of the input a (10 -5<a<10 5).

Exit Print the predecessor of the.

I did it like this, but it’s not working.

def antecessor(a):
    ant = a - 1
    print(ant)
    
a = int(input())
antecessor(a)
  • I ran your code, for me it worked. What error message is getting?

  • If enunciation is exactly this ...Create a function called predecessor that takes as a single parameter an integer a and prints its predecessor.... there is nothing wrong with your code. Now if the statement is something slightly different, example ...Create a function called predecessor that takes as a single parameter an integer to and returns its predecessor.... then you have a problem.

2 answers

1

It’s hard to understand without the error code, but I’ll try to help:

You could use Loop While thus:

def read_input(num):
    while True:
        try:
            c = int(input(num))
        except (ValueError, TypeError):
            print("\033[31mERRO! Por favor digite um numero inteiro...\033[m")
            continue
        except (KeyboardInterrupt):
            print("\033[31mEntrada de valor interrompida, por favor, reinicie o programa\033[31m")
            break
        else:
            return c


while True:
    numero = read_input('Digite um numero: ')
    antecessor = numero - 1
    print(f'O antecessor de seu numero é {antecessor}')

In this example I also used the function created to handle errors if the user type a variable other than int, read_input(). But it does not need to be used, as in this example:

while True:
    numero = int(input('Digite um numero: '))
    antecessor = numero - 1
    print(f'O antecessor de seu numero é {antecessor}')

Basically the while loop is being used only to keep the program running, but it is not necessary:

numero = int(input('Digite um numero: '))
antecessor = numero - 1
print(f'O antecessor de seu numero é {antecessor}')

Using the job creation

def calc_antecessor():
    numero = int(input('Digite um numero: '))
    antecessor = numero - 1
    print(f'O antecessor de seu numero é {antecessor}')


calc_antecessor()

You could also use the while loop here, placing it before calling the function

You could put everything together like creating functions, loop while and error handling in this way:

def read_input(num):
    while True:
        try:
            c = int(input(num))
        except (ValueError, TypeError):
            print("\033[31mERRO! Por favor digite um numero inteiro...\033[m")
            continue
        except (KeyboardInterrupt):
            print("\033[31mEntrada de valor interrompida, por favor, reinicie o programa\033[31m")
            break
        else:
            return c

def calc_antecessor():
    numero = read_input('Digite um numero: ')
    antecessor = numero - 1
    print(f'O antecessor de seu numero é {antecessor}')

while True:
    calc_antecessor()

0

a = int(input('Digite um número: '))
b = a - 1
print('O antecessor de ', a, ' é ', b)

Would that be?

Browser other questions tagged

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