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()
I ran your code, for me it worked. What error message is getting?
– Danizavtz
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.
– Augusto Vasques