Ask Your Question
0

'int' object has no attribute 'digits'

asked 2023-06-10 04:55:08 +0100

mcmug gravatar image

Hello all,

When I run the following two lines of code,

for j in range(3,4):
   print j.digits(3)

I get the error message

AttributeError: 'int' object has no attribute 'digits'

However, the following two lines give the expected outcome [0,1]:

j=3 
j.digits(3)

What can be wrong?

Thanks in advance!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2023-06-10 18:46:10 +0100

updated 2023-06-10 18:47:02 +0100

The issue is that the range function outputs numbers of the type Python int rather than Sage Integer, and digits is only defined for the latter. You could use Sage's srange function instead:

for i in range(2):
    print(type(i))

will produce

<class 'int'>
<class 'int'>

whereas

for i in srange(2):
    print(type(i))

will give

<class 'sage.rings.integer.Integer'>
<class 'sage.rings.integer.Integer'>
edit flag offensive delete link more
1

answered 2023-06-10 07:17:28 +0100

tolga gravatar image

This should work:

for j in range(3,4):
    print(Integer(j).digits(3))
edit flag offensive delete link more

Comments

It works. Thanks!

mcmug gravatar imagemcmug ( 2023-06-10 18:08:31 +0100 )edit

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: 2023-06-10 04:55:08 +0100

Seen: 561 times

Last updated: Jun 10 '23