Ask Your Question
0

sage thinks a float is a string

asked 2017-06-04 02:19:38 +0100

cybervigilante gravatar image

updated 2017-06-04 02:26:09 +0100

x = 42.44444

print("all {2:.3f} good {0} eat {1}".format("dogs","shoes",x))

ValueError: Unknown format code 'f' for object of type 'str

This is Sage in Linux Mint

edit retag flag offensive close merge delete

Comments

BTW, why am I not getting line breaks between the x value and the print. I tried preformat and I still don't get them.

cybervigilante gravatar imagecybervigilante ( 2017-06-04 02:21:58 +0100 )edit

In my experience, you can't have a code block as the first thing.

John Palmieri gravatar imageJohn Palmieri ( 2017-06-04 18:16:41 +0100 )edit

Correct, that is perhaps a bug in the markdown (https://github.com/trentm/python-mark...) similar to https://github.com/trentm/python-mark... which is noted at https://askbot.org/en/question/2732/f...

kcrisman gravatar imagekcrisman ( 2017-06-05 20:35:51 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-06-04 11:12:17 +0100

ndomes gravatar image

This works for me:

print("all {4:{0}.{1}} good {2} eat {3}".format(2,6,"dogs","shoes",x))
edit flag offensive delete link more

Comments

Or print("all {2:2.6} good {0} eat {1}".format("dogs","shoes",x))

John Palmieri gravatar imageJohn Palmieri ( 2017-06-04 18:20:44 +0100 )edit
0

answered 2017-06-04 18:19:23 +0100

This works for me:

x = float(42.44444)
print("all {2:.3f} good {0} eat {1}".format("dogs","shoes",x))

With your code, x is not a float:

x = 42.44444
sage: type(x)
<type 'sage.rings.real_mpfr.RealLiteral'>

I guess not all aspects of Python's format are implemented for this type of object. Perhaps it passes the string representation of x to format, which is what leads to the error message.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-06-04 02:19:38 +0100

Seen: 918 times

Last updated: Jun 04 '17