Typeerror: not all Arguments converted During string formatting (Python 3.4)

Asked

Viewed 8,700 times

13

I’m new to programming and I’m trying to do a simple code for an exercise to calculate a phone bill:

t = float(input('Digite a quantidade de minutos gasta: '))

if t < 200:
    p = t * 0,2
if t >= 200 and t < 400:
    p = t * 0,18
if t >= 400:
    p = t * 0,15

print('O preço da ligação foi de .2f reais.' %p)

However, it is returning the following error:

print('O preço da ligação foi de .2f reais.' %p)
TypeError: not all arguments converted during string formatting

What should I do? Thanks in advance.

3 answers

6


We’re missing a % in your string 'O preço da ligação foi de .2f reais.', therefore the .2f ends up being interpreted literally (i.e. will be part of the final string, without modifications) and - as "left" an argument - it warns you of the fact. In other words, your string expects zero arguments, and you pass one.

To correct this error, add the % that was missing:

'O preço da ligação foi de %.2f reais.'

And, as suggested in the other answers, correct also the inappropriate use of the comma, because the point is expected as decimal separator (the comma serves to create a tuple, and that’s what’s being stored in your variable p). I also agree with the suggestion to use the new formatting pattern, which uses the function format, instead of the operator % (is more organized, has more features, more formatting options and passage of arguments, etc).

  • Wow, those were pretty simple mistakes, and I didn’t even notice, rs. As for operators, I find it more practical to use '%' instead of 'format' when I don’t need to repeat the arguments in the string. But that’s for now, maybe in the future I’ll get used to 'format''.

4

First there is a previous error complicating the operation. Programming languages usually use American numerical notation so the separation of decimals is done with a dot and not a comma as it is in the code. Fix this.

Python has a new way to format data, prefer this form:

"O preço da ligação foi de {0:8.2f} reais.".format(p)

And now there’s something new called f-string.

3

As already mentioned the correct is to represent these floating values with a point . and not a comma ,. Behold here the problems and limitations of floating values in Python.

Your code should look like this:

t = int(input("Digite a quantidade de minutos gasta: "))

if t < 200:
    p = t * 0.2
if t >= 200 and t < 400:
    p = t * 0.18
if t >= 400:
    p = t * 0.15

print ("O preco da ligacao foi de %.2f reais." % p) 
# Ou com a funcao format()
print ("O preco da ligacao foi de {0:.2f} reais".format(p)) 

DEMO

Depending on the factor localization, the decimal separator may be different instead of a point . can be a comma ,. To obtain this information you can use the function nl_langinfo module locale with the option RADIXCHAR.

import locale

print (locale.nl_langinfo(locale.RADIXCHAR))

If it is necessary to calculate the values using decimal separator to , you can use the function atof to convert a string in a floating value. See a demo:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import locale

# Em pt_BR vai o separador decimal é "."
print (locale.nl_langinfo(locale.RADIXCHAR)) 
# Mudamos o locale para Inglês - Dinamarca
locale.setlocale(locale.LC_ALL, 'en_DK.utf8')
# Em en_DK o separador decimal é ","
print (locale.nl_langinfo(locale.RADIXCHAR))

t = int(input("Digite a quantidade de minutos gasta: "))

if t < 200:
    p = t * locale.atof("0,2")
if t >= 200 and t < 400:
    p = t * locale.atof("0,18")
if t >= 400:
    p = t * locale.atof("0,15")

print ("O preco da ligação foi de %.2f reais." % p) 
# Ou com a função format()
print ("O preco da ligacao foi de {0:.2f} reais".format(p)) 

Browser other questions tagged

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