How to split integer and assigning parts to variablesSplitting integer in Python?Getting only element from a single-element list in Python?How do I check whether a file exists without exceptions?How do JavaScript closures work?How do I generate random integers within a specific range in Java?How do I parse a string to a float or int?Using global variables in a functionHow do you check if a variable is an array in JavaScript?How do I pass a variable by reference?“Least Astonishment” and the Mutable Default ArgumentConvert a string to an integer?How to determine if variable is 'undefined' or 'null'?
Global variables and information security
Which costing factors go into the optimizer choosing different types of spools?
What is the source of the fear in the Hallow spell's extra Fear effect?
What is the majority of the UK Government as of 2019-09-04?
Entering the US with dual citizenship but US passport is long expired?
How does the UK House of Commons think they can prolong the deadline of Brexit?
Is the interior of a Bag of Holding actually an extradimensional space?
Shoes for commuting
Why are all volatile liquids combustible
Left my gmail logged in when I was fired
What is the difference between 「名前【なまえ】」 and 「名称【めいしょう】」?
Are buttons really enough to bound validities by S4.2?
In-universe, why does Doc Brown program the time machine to go to 1955?
Is the Levitate spell supposed to basically disable a melee-based enemy?
Tiny image scraper for xkcd.com
Is there any reason to change the ISO manually?
If I sell my PS4 game disc and buy a digital version, can I still access my saved game?
split a six digits number column into separated columns with one digit
Who are these people in this satirical cartoon of the Congress of Verona?
Why does the seven segment display have decimal point at the right?
MOSFET broke after attaching capacitor bank
Tying double knot of garbarge bag
Vimscript - Surround word under cursor with quotes
A Meal fit for a King
How to split integer and assigning parts to variables
Splitting integer in Python?Getting only element from a single-element list in Python?How do I check whether a file exists without exceptions?How do JavaScript closures work?How do I generate random integers within a specific range in Java?How do I parse a string to a float or int?Using global variables in a functionHow do you check if a variable is an array in JavaScript?How do I pass a variable by reference?“Least Astonishment” and the Mutable Default ArgumentConvert a string to an integer?How to determine if variable is 'undefined' or 'null'?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have been recently working on code that is a calculator. I know calculations can be done in the shell but that's no fun. Anyway, I have successfully been able to separate the numbers from the equation so that I can add them together. However it outputs like so:
Numbers: 22
This happens with the equation 2+2.
What I want to happen is it to take this integer (22) and separate it into 2 and 2 then assign those to variables "num1" and "num2" so that I can add them.
I have already tried:
[int(i) for i in str(Numbers)]
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
I have looked at this:
Splitting integer in Python?
That is what got me my output above. I have also looked at this:
Getting only element from a single-element list in Python?
But I didn't understand that and don't have a high enough reputation score to comment and ask for explanation.
This is my current code as it currently stands:
var = input("Type equation:")
if " + " in var:
nums = str(re.findall(r'd',var))
nums2 = nums.replace("['", "")
nums3 = nums2.replace("', '", "")
Numbers = nums3.replace("']", "")
print(Numbers)
python variables integer
add a comment |
I have been recently working on code that is a calculator. I know calculations can be done in the shell but that's no fun. Anyway, I have successfully been able to separate the numbers from the equation so that I can add them together. However it outputs like so:
Numbers: 22
This happens with the equation 2+2.
What I want to happen is it to take this integer (22) and separate it into 2 and 2 then assign those to variables "num1" and "num2" so that I can add them.
I have already tried:
[int(i) for i in str(Numbers)]
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
I have looked at this:
Splitting integer in Python?
That is what got me my output above. I have also looked at this:
Getting only element from a single-element list in Python?
But I didn't understand that and don't have a high enough reputation score to comment and ask for explanation.
This is my current code as it currently stands:
var = input("Type equation:")
if " + " in var:
nums = str(re.findall(r'd',var))
nums2 = nums.replace("['", "")
nums3 = nums2.replace("', '", "")
Numbers = nums3.replace("']", "")
print(Numbers)
python variables integer
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21
add a comment |
I have been recently working on code that is a calculator. I know calculations can be done in the shell but that's no fun. Anyway, I have successfully been able to separate the numbers from the equation so that I can add them together. However it outputs like so:
Numbers: 22
This happens with the equation 2+2.
What I want to happen is it to take this integer (22) and separate it into 2 and 2 then assign those to variables "num1" and "num2" so that I can add them.
I have already tried:
[int(i) for i in str(Numbers)]
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
I have looked at this:
Splitting integer in Python?
That is what got me my output above. I have also looked at this:
Getting only element from a single-element list in Python?
But I didn't understand that and don't have a high enough reputation score to comment and ask for explanation.
This is my current code as it currently stands:
var = input("Type equation:")
if " + " in var:
nums = str(re.findall(r'd',var))
nums2 = nums.replace("['", "")
nums3 = nums2.replace("', '", "")
Numbers = nums3.replace("']", "")
print(Numbers)
python variables integer
I have been recently working on code that is a calculator. I know calculations can be done in the shell but that's no fun. Anyway, I have successfully been able to separate the numbers from the equation so that I can add them together. However it outputs like so:
Numbers: 22
This happens with the equation 2+2.
What I want to happen is it to take this integer (22) and separate it into 2 and 2 then assign those to variables "num1" and "num2" so that I can add them.
I have already tried:
[int(i) for i in str(Numbers)]
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
I have looked at this:
Splitting integer in Python?
That is what got me my output above. I have also looked at this:
Getting only element from a single-element list in Python?
But I didn't understand that and don't have a high enough reputation score to comment and ask for explanation.
This is my current code as it currently stands:
var = input("Type equation:")
if " + " in var:
nums = str(re.findall(r'd',var))
nums2 = nums.replace("['", "")
nums3 = nums2.replace("', '", "")
Numbers = nums3.replace("']", "")
print(Numbers)
python variables integer
python variables integer
edited Mar 28 at 4:14
Char Gamer
asked Mar 28 at 4:09
Char GamerChar Gamer
105 bronze badges
105 bronze badges
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21
add a comment |
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21
add a comment |
2 Answers
2
active
oldest
votes
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
Use access through index:
num1, num2 = [int(i) for i in str(Numbers)] [0], [int(i) for i in str(Numbers)] [1]
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply donum1, num2 = (int(i) for i in str(Numbers))
.
– iz_
Mar 28 at 4:26
add a comment |
Assign your output to two variables num1, num2
num1,num2=list(str(Numbers))
You don't need thelist(str())
,str()
is enough, i.e.num1, num2 = str(Numbers)
. However, they will be strings, not integers.
– iz_
Mar 28 at 4:20
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%2f55390028%2fhow-to-split-integer-and-assigning-parts-to-variables%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
Use access through index:
num1, num2 = [int(i) for i in str(Numbers)] [0], [int(i) for i in str(Numbers)] [1]
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply donum1, num2 = (int(i) for i in str(Numbers))
.
– iz_
Mar 28 at 4:26
add a comment |
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
Use access through index:
num1, num2 = [int(i) for i in str(Numbers)] [0], [int(i) for i in str(Numbers)] [1]
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply donum1, num2 = (int(i) for i in str(Numbers))
.
– iz_
Mar 28 at 4:26
add a comment |
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
Use access through index:
num1, num2 = [int(i) for i in str(Numbers)] [0], [int(i) for i in str(Numbers)] [1]
But the output is in a list and I have not found anything about taking a list item and assigning it to a variable.
Use access through index:
num1, num2 = [int(i) for i in str(Numbers)] [0], [int(i) for i in str(Numbers)] [1]
answered Mar 28 at 4:20
Saurav SahuSaurav Sahu
7,1843 gold badges30 silver badges47 bronze badges
7,1843 gold badges30 silver badges47 bronze badges
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply donum1, num2 = (int(i) for i in str(Numbers))
.
– iz_
Mar 28 at 4:26
add a comment |
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply donum1, num2 = (int(i) for i in str(Numbers))
.
– iz_
Mar 28 at 4:26
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
This redoes the same operation twice unnecessarily. Storing the result or using unpacking is a better idea.
– iz_
Mar 28 at 4:21
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
Yes, to optimize that he can populate the list into a local variable and later access through index.
– Saurav Sahu
Mar 28 at 4:22
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
THANKS! This helped out so much! I got it working!
– Char Gamer
Mar 28 at 4:25
To simplify the code, simply do
num1, num2 = (int(i) for i in str(Numbers))
.– iz_
Mar 28 at 4:26
To simplify the code, simply do
num1, num2 = (int(i) for i in str(Numbers))
.– iz_
Mar 28 at 4:26
add a comment |
Assign your output to two variables num1, num2
num1,num2=list(str(Numbers))
You don't need thelist(str())
,str()
is enough, i.e.num1, num2 = str(Numbers)
. However, they will be strings, not integers.
– iz_
Mar 28 at 4:20
add a comment |
Assign your output to two variables num1, num2
num1,num2=list(str(Numbers))
You don't need thelist(str())
,str()
is enough, i.e.num1, num2 = str(Numbers)
. However, they will be strings, not integers.
– iz_
Mar 28 at 4:20
add a comment |
Assign your output to two variables num1, num2
num1,num2=list(str(Numbers))
Assign your output to two variables num1, num2
num1,num2=list(str(Numbers))
answered Mar 28 at 4:15
SravanthiGSravanthiG
513 bronze badges
513 bronze badges
You don't need thelist(str())
,str()
is enough, i.e.num1, num2 = str(Numbers)
. However, they will be strings, not integers.
– iz_
Mar 28 at 4:20
add a comment |
You don't need thelist(str())
,str()
is enough, i.e.num1, num2 = str(Numbers)
. However, they will be strings, not integers.
– iz_
Mar 28 at 4:20
You don't need the
list(str())
, str()
is enough, i.e. num1, num2 = str(Numbers)
. However, they will be strings, not integers.– iz_
Mar 28 at 4:20
You don't need the
list(str())
, str()
is enough, i.e. num1, num2 = str(Numbers)
. However, they will be strings, not integers.– iz_
Mar 28 at 4:20
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%2f55390028%2fhow-to-split-integer-and-assigning-parts-to-variables%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
FYI, a dynamic number of variables is almost never a good idea. What's wrong with a list?
– iz_
Mar 28 at 4:19
What is your input, your output and expected output?
– Saurav Sahu
Mar 28 at 4:21