TypeError: object of type 'int' has no len() *subtraction*What's the canonical way to check for type in Python?Generating random numbers in Objective-CHow do I parse a string to a float or int?How to determine a Python variable's type?How to know if an object has an attribute in PythonShuffling a list of objectsWhat are the differences between type() and isinstance()?Determine the type of an object?How do I generate a random int number?Run a Python script from another Python script, passing in arguments
Would a 7805 5v regulator drain a 9v battery?
Converting 3x7 to a 1x7. Is it possible with only existing parts?
When is the phrase "j'ai bon" used?
Interview was just a one hour panel. Got an offer the next day; do I accept or is this a red flag?
Is swap gate equivalent to just exchanging the wire of the two qubits?
2 Managed Packages in 1 Dev Org
Does anyone recognize these rockets, and their location?
Why can't we feel the Earth's revolution?
What is the color associated with lukewarm?
Lead the way to this Literary Knight to its final “DESTINATION”
How did space travel spread through the galaxy?
Fill the maze with a wall-following Snake until it gets stuck
Is my research statement supposed to lead to papers in top journals?
High-end PC graphics circa 1990?
My student in one course asks for paid tutoring in another course. Appropriate?
Are there examples of rowers who also fought?
Why is Skinner so awkward in Hot Fuzz?
How to know whether to write accidentals as sharps or flats?
What is this plant I saw for sale at a Romanian farmer's market?
What are the mechanical differences between Adapt and Monstrosity?
Manager wants to hire me; HR does not. How to proceed?
Is there any effect in D&D 5e that cannot be undone?
Why can't I craft scaffolding in Minecraft 1.14?
Are there any super-powered aliens in the Marvel universe?
TypeError: object of type 'int' has no len() *subtraction*
What's the canonical way to check for type in Python?Generating random numbers in Objective-CHow do I parse a string to a float or int?How to determine a Python variable's type?How to know if an object has an attribute in PythonShuffling a list of objectsWhat are the differences between type() and isinstance()?Determine the type of an object?How do I generate a random int number?Run a Python script from another Python script, passing in arguments
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4
number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.
The desired output would be something like this:
0617 or 0071 or 0008, etc.
Please help with this issue, thanks!
python python-3.x random
add a comment |
I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4
number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.
The desired output would be something like this:
0617 or 0071 or 0008, etc.
Please help with this issue, thanks!
python python-3.x random
add a comment |
I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4
number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.
The desired output would be something like this:
0617 or 0071 or 0008, etc.
Please help with this issue, thanks!
python python-3.x random
I'm trying to figure out if there's a way to make a small calculation by subtracting a variables number from 4
number_1_raw = random.randrange(1, 1000)
number_1_len = len(number_1_raw)
number_of_zeros = 4 - number_1_len
print("0" * number_of_zeros + number_1_raw)
I wanted the script to give me an answer that ranged from 1-3, however the
code isn't executing properly.
The desired output would be something like this:
0617 or 0071 or 0008, etc.
Please help with this issue, thanks!
python python-3.x random
python python-3.x random
edited Mar 25 at 4:25
AkshayNevrekar
6,831102243
6,831102243
asked Mar 25 at 4:11
Moses FordMoses Ford
132
132
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
add a comment |
This line number_1_len = len(number_1_raw)
is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
add a comment |
Fist all I need to understands better what are happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...
add a comment |
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw
which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len
on it.
Furthermore, in print
statement you need to convert number_1_raw
into string so that it can be appended. You can not perform +
operation on str
and int
.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331164%2ftypeerror-object-of-type-int-has-no-len-subtraction%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
add a comment |
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
add a comment |
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
You cant call len on an integer.
The len() function returns the number of items in an object.
When the object is a string, the len() function returns the number of
characters in the string.
so what you can do is
number_1_len = len(str(number_1_raw))
str changes int type to string type.
answered Mar 25 at 4:16
Jibin MathewsJibin Mathews
629616
629616
add a comment |
add a comment |
This line number_1_len = len(number_1_raw)
is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
add a comment |
This line number_1_len = len(number_1_raw)
is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
add a comment |
This line number_1_len = len(number_1_raw)
is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
This line number_1_len = len(number_1_raw)
is problem. You are getting an integer value as number_1_len, and it has length of one. This number_1_len is actually the number of zeros that you want
answered Mar 25 at 4:18
nhrcptnhrcpt
411624
411624
add a comment |
add a comment |
Fist all I need to understands better what are happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...
add a comment |
Fist all I need to understands better what are happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...
add a comment |
Fist all I need to understands better what are happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...
Fist all I need to understands better what are happening...
Fist
number_1_raw = random.randrange(1, 1000)
The random.randrange returns a number, probably int.
Second
number_1_len = len(number_1_raw)
Has a problem, because number_1_raw is an int and len is a function that count the number of elements from an array, an object... but number_1_len isn't... that's an int and len can't count an int element...
Is important you show all which error you're getting... and explain better what you're trying to do...
answered Mar 25 at 4:34
GuilhermeGuilherme
218
218
add a comment |
add a comment |
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw
which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len
on it.
Furthermore, in print
statement you need to convert number_1_raw
into string so that it can be appended. You can not perform +
operation on str
and int
.
add a comment |
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw
which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len
on it.
Furthermore, in print
statement you need to convert number_1_raw
into string so that it can be appended. You can not perform +
operation on str
and int
.
add a comment |
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw
which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len
on it.
Furthermore, in print
statement you need to convert number_1_raw
into string so that it can be appended. You can not perform +
operation on str
and int
.
I think you need:
import random
number_1_raw = random.randrange(1, 1000)
number_1_len = len(str(number_1_raw))
number_of_zeros = 4 - number_1_len
print(number_of_zeros)
result = "0" * number_of_zeros
print(result+str(number_1_raw))
Output
1 # number of zeros
0801
What's wrong with your code
You are trying to find length of an integer number_1_raw
which is giving you the error as you need iterables or string to find a length of that object. First convert it into string and then use len
on it.
Furthermore, in print
statement you need to convert number_1_raw
into string so that it can be appended. You can not perform +
operation on str
and int
.
edited Mar 25 at 4:37
answered Mar 25 at 4:23
AkshayNevrekarAkshayNevrekar
6,831102243
6,831102243
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55331164%2ftypeerror-object-of-type-int-has-no-len-subtraction%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown