Typeerror: not all Arguments converted During string formatting (python)

Asked

Viewed 336 times

0

I’m new to programming, I’m trying to make a program that checks if a number inserted is prime, so I’m checking if the rest of the division of number by primes is zero.

def primo():

numero = input("Digite seu numero: ")

    if numero%2 == 0:
        print ("Seu numero não é primo")
    elif numero %3 == 0:
        print ("Seu numero não é primo")
    elif numero %5 == 0:
        print ("Seu numero não é primo")
    elif numero %7 == 0:
        print ("Seu numero não é primo")
    else:
        print ("Seu numero é primo")

but I’m getting Typeerror: not all Arguments converted During string formatting in the first "if" line. What I’m doing wrong ?

2 answers

1

It turns out that the function return input is always a string:

input([prompt])

If the prompt argument is present, it is Written to standard output without a trailing newline. The Function then reads a line from input, converts it to a string (stripping a trailing newline), and Returns that. When EOF is read, Eoferror is Raised.

And in Python the guy string owns the operator % who does the formatting. For example, if you do '%s %s' % ('Anderson', 'Woss') will have the string final 'Anderson Woss'. About the operator and other ways to format a string see:

As the intention is to work with numbers, just create an instance of int from their string:

numero = int(input('...'))

And thus the operator % will split rest as expected and no longer a formatting operator.

1

Just convert to entire your input

numero = int(input("Digite seu numero: "))

Browser other questions tagged

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