Python: Why is entry set to the value of 1
I am new to Python and I am trying to figure out why the value of entry
in the following code is set to "1" at the top of the code, and why when I press "0" as the first and only digit when testing the code the "sum" gets to be "0" and not "1" when the "entry" is already bound up with number of “1”.
Also, what is entry
called? A function?
Best regards
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
python-3.x
add a comment |
I am new to Python and I am trying to figure out why the value of entry
in the following code is set to "1" at the top of the code, and why when I press "0" as the first and only digit when testing the code the "sum" gets to be "0" and not "1" when the "entry" is already bound up with number of “1”.
Also, what is entry
called? A function?
Best regards
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
python-3.x
total
is initialised to 0, and entry is overwritten byinput
before it is added the first time - the initial value of1
is only required to pass the test inwhile
.entry
is called a variable or name.
– MisterMiyagi
Nov 21 '18 at 6:24
add a comment |
I am new to Python and I am trying to figure out why the value of entry
in the following code is set to "1" at the top of the code, and why when I press "0" as the first and only digit when testing the code the "sum" gets to be "0" and not "1" when the "entry" is already bound up with number of “1”.
Also, what is entry
called? A function?
Best regards
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
python-3.x
I am new to Python and I am trying to figure out why the value of entry
in the following code is set to "1" at the top of the code, and why when I press "0" as the first and only digit when testing the code the "sum" gets to be "0" and not "1" when the "entry" is already bound up with number of “1”.
Also, what is entry
called? A function?
Best regards
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
python-3.x
python-3.x
edited Nov 21 '18 at 6:33
Sociopath
3,77791635
3,77791635
asked Nov 21 '18 at 6:18
Finn NoobFinn Noob
113
113
total
is initialised to 0, and entry is overwritten byinput
before it is added the first time - the initial value of1
is only required to pass the test inwhile
.entry
is called a variable or name.
– MisterMiyagi
Nov 21 '18 at 6:24
add a comment |
total
is initialised to 0, and entry is overwritten byinput
before it is added the first time - the initial value of1
is only required to pass the test inwhile
.entry
is called a variable or name.
– MisterMiyagi
Nov 21 '18 at 6:24
total
is initialised to 0, and entry is overwritten by input
before it is added the first time - the initial value of 1
is only required to pass the test in while
. entry
is called a variable or name.– MisterMiyagi
Nov 21 '18 at 6:24
total
is initialised to 0, and entry is overwritten by input
before it is added the first time - the initial value of 1
is only required to pass the test in while
. entry
is called a variable or name.– MisterMiyagi
Nov 21 '18 at 6:24
add a comment |
4 Answers
4
active
oldest
votes
entry
is a variable.
It has been set to 1 to enter into the while loop
. For while loop to run you need condition to be True
. So it evaluates 1 > 0
which is True
and enters into the loop.
As value of entry
is overriden in while loop, it takes the value from user input.
As you said if you press 0 the value of entry will become 0 instead of 1 and the will get added to sum
which is 0 so 0+0
will return 0.
You can set it to any value greater than Zero.
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
add a comment |
Here Entry
is basically used as a flag, which is set to True
so that the While
loop can run continuously untill 0(zero) is passed as a False
value for Entry
variable, which i turns than stops the While
loop and return Total Sum.
add a comment |
entry
is variable. It is 1 after entry = 1
. So first time inside while
loop entry > 0
is true and you go inside loop and read one number, and assign it to entry. Now if you enter 0
then entry
is 0, and 0+0 is 0, so total
is 0, and entry > 0
is false, so you fall out of while
loop and print: "The sum of all numbers =0`.
But but but: question "why is entry set to value of 1" I cannot answer. You must go ask person who wrote this code and ask, "why is entry set to value of 1?" and maybe then you get answer. But I cannot give you that answer.
To me it looks like silly code written for silly reasons. I don't know.
add a comment |
The initial value of entry
is set 1 and on the other hand you are also taking input
with the same variable. So, whenever you are putting 0 on it the entry
variable becoming 0 as initial value.
total
is already set as 0 and it add the entry
value which is also 0 so the result become 0.
So when the loop
goes back & check its condition entry > 0 becoming 0 > 0
which is false.
so the code does something like this,
You Code
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
First Attempt
entry = 1
total = 0
while entry > 0: # 1 > 0 because entry = 1
entry = int(input('Enter a number: ')) # entry is becoming 0
total += entry # total = 0, because entry is 0 now
print('The sum of all numbers =', total)
Last Attempt
entry = 1
total = 0
while entry > 0: # 0 > 0 False because entry = 0
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
Output
>>> The sum of all numbers = 0
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%2f53406263%2fpython-why-is-entry-set-to-the-value-of-1%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
entry
is a variable.
It has been set to 1 to enter into the while loop
. For while loop to run you need condition to be True
. So it evaluates 1 > 0
which is True
and enters into the loop.
As value of entry
is overriden in while loop, it takes the value from user input.
As you said if you press 0 the value of entry will become 0 instead of 1 and the will get added to sum
which is 0 so 0+0
will return 0.
You can set it to any value greater than Zero.
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
add a comment |
entry
is a variable.
It has been set to 1 to enter into the while loop
. For while loop to run you need condition to be True
. So it evaluates 1 > 0
which is True
and enters into the loop.
As value of entry
is overriden in while loop, it takes the value from user input.
As you said if you press 0 the value of entry will become 0 instead of 1 and the will get added to sum
which is 0 so 0+0
will return 0.
You can set it to any value greater than Zero.
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
add a comment |
entry
is a variable.
It has been set to 1 to enter into the while loop
. For while loop to run you need condition to be True
. So it evaluates 1 > 0
which is True
and enters into the loop.
As value of entry
is overriden in while loop, it takes the value from user input.
As you said if you press 0 the value of entry will become 0 instead of 1 and the will get added to sum
which is 0 so 0+0
will return 0.
You can set it to any value greater than Zero.
entry
is a variable.
It has been set to 1 to enter into the while loop
. For while loop to run you need condition to be True
. So it evaluates 1 > 0
which is True
and enters into the loop.
As value of entry
is overriden in while loop, it takes the value from user input.
As you said if you press 0 the value of entry will become 0 instead of 1 and the will get added to sum
which is 0 so 0+0
will return 0.
You can set it to any value greater than Zero.
edited Nov 21 '18 at 6:31
answered Nov 21 '18 at 6:23
SociopathSociopath
3,77791635
3,77791635
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
add a comment |
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
2
2
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
Thanks, exactly what i was looking for!
– Finn Noob
Nov 21 '18 at 6:33
1
1
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
If it helped accept the answer.
– Sociopath
Nov 21 '18 at 6:34
add a comment |
Here Entry
is basically used as a flag, which is set to True
so that the While
loop can run continuously untill 0(zero) is passed as a False
value for Entry
variable, which i turns than stops the While
loop and return Total Sum.
add a comment |
Here Entry
is basically used as a flag, which is set to True
so that the While
loop can run continuously untill 0(zero) is passed as a False
value for Entry
variable, which i turns than stops the While
loop and return Total Sum.
add a comment |
Here Entry
is basically used as a flag, which is set to True
so that the While
loop can run continuously untill 0(zero) is passed as a False
value for Entry
variable, which i turns than stops the While
loop and return Total Sum.
Here Entry
is basically used as a flag, which is set to True
so that the While
loop can run continuously untill 0(zero) is passed as a False
value for Entry
variable, which i turns than stops the While
loop and return Total Sum.
answered Nov 21 '18 at 6:51
Akshit ChauhanAkshit Chauhan
113
113
add a comment |
add a comment |
entry
is variable. It is 1 after entry = 1
. So first time inside while
loop entry > 0
is true and you go inside loop and read one number, and assign it to entry. Now if you enter 0
then entry
is 0, and 0+0 is 0, so total
is 0, and entry > 0
is false, so you fall out of while
loop and print: "The sum of all numbers =0`.
But but but: question "why is entry set to value of 1" I cannot answer. You must go ask person who wrote this code and ask, "why is entry set to value of 1?" and maybe then you get answer. But I cannot give you that answer.
To me it looks like silly code written for silly reasons. I don't know.
add a comment |
entry
is variable. It is 1 after entry = 1
. So first time inside while
loop entry > 0
is true and you go inside loop and read one number, and assign it to entry. Now if you enter 0
then entry
is 0, and 0+0 is 0, so total
is 0, and entry > 0
is false, so you fall out of while
loop and print: "The sum of all numbers =0`.
But but but: question "why is entry set to value of 1" I cannot answer. You must go ask person who wrote this code and ask, "why is entry set to value of 1?" and maybe then you get answer. But I cannot give you that answer.
To me it looks like silly code written for silly reasons. I don't know.
add a comment |
entry
is variable. It is 1 after entry = 1
. So first time inside while
loop entry > 0
is true and you go inside loop and read one number, and assign it to entry. Now if you enter 0
then entry
is 0, and 0+0 is 0, so total
is 0, and entry > 0
is false, so you fall out of while
loop and print: "The sum of all numbers =0`.
But but but: question "why is entry set to value of 1" I cannot answer. You must go ask person who wrote this code and ask, "why is entry set to value of 1?" and maybe then you get answer. But I cannot give you that answer.
To me it looks like silly code written for silly reasons. I don't know.
entry
is variable. It is 1 after entry = 1
. So first time inside while
loop entry > 0
is true and you go inside loop and read one number, and assign it to entry. Now if you enter 0
then entry
is 0, and 0+0 is 0, so total
is 0, and entry > 0
is false, so you fall out of while
loop and print: "The sum of all numbers =0`.
But but but: question "why is entry set to value of 1" I cannot answer. You must go ask person who wrote this code and ask, "why is entry set to value of 1?" and maybe then you get answer. But I cannot give you that answer.
To me it looks like silly code written for silly reasons. I don't know.
answered Nov 21 '18 at 6:22
user10631003
add a comment |
add a comment |
The initial value of entry
is set 1 and on the other hand you are also taking input
with the same variable. So, whenever you are putting 0 on it the entry
variable becoming 0 as initial value.
total
is already set as 0 and it add the entry
value which is also 0 so the result become 0.
So when the loop
goes back & check its condition entry > 0 becoming 0 > 0
which is false.
so the code does something like this,
You Code
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
First Attempt
entry = 1
total = 0
while entry > 0: # 1 > 0 because entry = 1
entry = int(input('Enter a number: ')) # entry is becoming 0
total += entry # total = 0, because entry is 0 now
print('The sum of all numbers =', total)
Last Attempt
entry = 1
total = 0
while entry > 0: # 0 > 0 False because entry = 0
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
Output
>>> The sum of all numbers = 0
add a comment |
The initial value of entry
is set 1 and on the other hand you are also taking input
with the same variable. So, whenever you are putting 0 on it the entry
variable becoming 0 as initial value.
total
is already set as 0 and it add the entry
value which is also 0 so the result become 0.
So when the loop
goes back & check its condition entry > 0 becoming 0 > 0
which is false.
so the code does something like this,
You Code
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
First Attempt
entry = 1
total = 0
while entry > 0: # 1 > 0 because entry = 1
entry = int(input('Enter a number: ')) # entry is becoming 0
total += entry # total = 0, because entry is 0 now
print('The sum of all numbers =', total)
Last Attempt
entry = 1
total = 0
while entry > 0: # 0 > 0 False because entry = 0
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
Output
>>> The sum of all numbers = 0
add a comment |
The initial value of entry
is set 1 and on the other hand you are also taking input
with the same variable. So, whenever you are putting 0 on it the entry
variable becoming 0 as initial value.
total
is already set as 0 and it add the entry
value which is also 0 so the result become 0.
So when the loop
goes back & check its condition entry > 0 becoming 0 > 0
which is false.
so the code does something like this,
You Code
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
First Attempt
entry = 1
total = 0
while entry > 0: # 1 > 0 because entry = 1
entry = int(input('Enter a number: ')) # entry is becoming 0
total += entry # total = 0, because entry is 0 now
print('The sum of all numbers =', total)
Last Attempt
entry = 1
total = 0
while entry > 0: # 0 > 0 False because entry = 0
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
Output
>>> The sum of all numbers = 0
The initial value of entry
is set 1 and on the other hand you are also taking input
with the same variable. So, whenever you are putting 0 on it the entry
variable becoming 0 as initial value.
total
is already set as 0 and it add the entry
value which is also 0 so the result become 0.
So when the loop
goes back & check its condition entry > 0 becoming 0 > 0
which is false.
so the code does something like this,
You Code
entry = 1
total = 0
while entry > 0:
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
First Attempt
entry = 1
total = 0
while entry > 0: # 1 > 0 because entry = 1
entry = int(input('Enter a number: ')) # entry is becoming 0
total += entry # total = 0, because entry is 0 now
print('The sum of all numbers =', total)
Last Attempt
entry = 1
total = 0
while entry > 0: # 0 > 0 False because entry = 0
entry = int(input('Enter a number: '))
total += entry
print('The sum of all numbers =', total)
Output
>>> The sum of all numbers = 0
answered Nov 21 '18 at 6:39
zxyzxy
35329
35329
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%2f53406263%2fpython-why-is-entry-set-to-the-value-of-1%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
total
is initialised to 0, and entry is overwritten byinput
before it is added the first time - the initial value of1
is only required to pass the test inwhile
.entry
is called a variable or name.– MisterMiyagi
Nov 21 '18 at 6:24