OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why is format() throwing ValueError: Unknown format code 'f' for object of type 'str' when I'm not inputting a string?

  • Thread starter Thread starter Réka
  • Start date Start date
R

Réka

Guest
I am using Python 2.7. (Switching to Python 3 for this particular code is not an option, please don't suggest it.) I am writing unit tests for some code.

Here is the relevant piece of code:

Code:
class SingleLineGrooveTable:
  VEFMT = '.3f'

  @classmethod
  def formatve(cls, value, error=None):
    er = 0
    if error is not None:
      er = error
      v = value
    elif len(value) > 1:
      v, er = value
    else:
      v = value
    return format(v, cls.VEFMT), format(er, cls.VEFMT)

and my test is:

Code:
import unittest

class TestSingleLineGrooveTable(unittest.TestCase):
  
  def test_formatve_no_error(self):
    e_v = '3.142'
    e_er = '0.000'
    r_v, r_er = SingleLineGrooveTable.formatve([3.1423])
    self.assertEqual(e_v, r_v)
    self.assertEqual(e_er, r_er)

(Yes, I know it's funny I'm getting an error on the test with "no_error" in the name...)

When I run the test, it throws ValueError: Unknown format code 'f' for object of type 'str' on the return statement for the function. But I can't figure out where it's getting a str from. Possibly relevant, this code and the code I have that uses it were copied pretty much wholesale from someone else's code (who I can no longer contact), so maybe I'm calling it in the wrong way, but still, that's a list, not a string!

What is going on here? How do I fix this?
<p>I am using Python 2.7. (Switching to Python 3 for this particular code is not an option, please don't suggest it.) I am writing unit tests for some code.</p>
<p>Here is the relevant piece of code:</p>
<pre><code>class SingleLineGrooveTable:
VEFMT = '.3f'

@classmethod
def formatve(cls, value, error=None):
er = 0
if error is not None:
er = error
v = value
elif len(value) > 1:
v, er = value
else:
v = value
return format(v, cls.VEFMT), format(er, cls.VEFMT)
</code></pre>
<p>and my test is:</p>
<pre><code>import unittest

class TestSingleLineGrooveTable(unittest.TestCase):

def test_formatve_no_error(self):
e_v = '3.142'
e_er = '0.000'
r_v, r_er = SingleLineGrooveTable.formatve([3.1423])
self.assertEqual(e_v, r_v)
self.assertEqual(e_er, r_er)
</code></pre>
<p>(Yes, I know it's funny I'm getting an error on the test with "no_error" in the name...)</p>
<p>When I run the test, it throws <code>ValueError: Unknown format code 'f' for object of type 'str'</code> on the return statement for the function. But I can't figure out where it's getting a str from. Possibly relevant, this code and the code I have that uses it were copied pretty much wholesale from someone else's code (who I can no longer contact), so maybe I'm calling it in the wrong way, but still, that's a list, not a string!</p>
<p>What is going on here? How do I fix this?</p>
 

Latest posts

Top