TypeError: not all arguments converted during string formatting (Python 3.4)

6

I'm new to programming and trying to make a simple exercise code 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.

    
asked by anonymous 22.02.2015 / 13:23

3 answers

4

A % is missing in its string 'O preço da ligação foi de .2f reais.' , so the .2f ends up being interpreted literally (ie it will be part of the final string, without modifications) and - as "left" an argument - it warns you of fact. In other words, your string expects zero arguments, and you passed one.

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

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

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

    
22.02.2015 / 21:06
4

First you have a previous error complicating the operation. Programming languages often use American numeric notation so the separation of decimal places is done with a period, not a comma as it is in the code. Please correct 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)
    
22.02.2015 / 13:39
3

As I have already mentioned, the correct thing is to represent these floating values with a% point of% and not a comma% with% . See here for 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 location factor, decimal separator may be different, rather than a % dot% can be a comma . . To obtain this information you can use the , function of the . with the , .

import locale

print (locale.nl_langinfo(locale.RADIXCHAR))

If you need to calculate the values having as decimal separator a nl_langinfo you can use the locale to convert a string to a floating value. Here's 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)) 
    
22.02.2015 / 15:00