python, using if loop to make sure user input is a number then manipulate number












1















I am very new to programming and tried to make a age checker code, however the loop to make sure user input is a number does not work.



If a string is entered, for example hello, the input passes the .isdigit() condition, and messes up the last if statement.



Any help would or advice on how to construct the number checker loop properly, so that strings are not entered into the last if statement would be greatly appreciated.
Here is my code:



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
else:
print('A number must be entered')
break
print('Please enter age')
used_variable = age
if used_variable > 18:
print('user is older than 18')









share|improve this question




















  • 1





    Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

    – leoburgy
    Nov 21 '18 at 13:19











  • are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

    – Avishay Cohen
    Nov 21 '18 at 14:29


















1















I am very new to programming and tried to make a age checker code, however the loop to make sure user input is a number does not work.



If a string is entered, for example hello, the input passes the .isdigit() condition, and messes up the last if statement.



Any help would or advice on how to construct the number checker loop properly, so that strings are not entered into the last if statement would be greatly appreciated.
Here is my code:



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
else:
print('A number must be entered')
break
print('Please enter age')
used_variable = age
if used_variable > 18:
print('user is older than 18')









share|improve this question




















  • 1





    Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

    – leoburgy
    Nov 21 '18 at 13:19











  • are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

    – Avishay Cohen
    Nov 21 '18 at 14:29
















1












1








1








I am very new to programming and tried to make a age checker code, however the loop to make sure user input is a number does not work.



If a string is entered, for example hello, the input passes the .isdigit() condition, and messes up the last if statement.



Any help would or advice on how to construct the number checker loop properly, so that strings are not entered into the last if statement would be greatly appreciated.
Here is my code:



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
else:
print('A number must be entered')
break
print('Please enter age')
used_variable = age
if used_variable > 18:
print('user is older than 18')









share|improve this question
















I am very new to programming and tried to make a age checker code, however the loop to make sure user input is a number does not work.



If a string is entered, for example hello, the input passes the .isdigit() condition, and messes up the last if statement.



Any help would or advice on how to construct the number checker loop properly, so that strings are not entered into the last if statement would be greatly appreciated.
Here is my code:



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
else:
print('A number must be entered')
break
print('Please enter age')
used_variable = age
if used_variable > 18:
print('user is older than 18')






python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 15:00









Avishay Cohen

701724




701724










asked Nov 21 '18 at 13:10









Jacob Jacob

91




91








  • 1





    Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

    – leoburgy
    Nov 21 '18 at 13:19











  • are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

    – Avishay Cohen
    Nov 21 '18 at 14:29
















  • 1





    Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

    – leoburgy
    Nov 21 '18 at 13:19











  • are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

    – Avishay Cohen
    Nov 21 '18 at 14:29










1




1





Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

– leoburgy
Nov 21 '18 at 13:19





Once you know that it only consists of digits you still have to cast your string into an integer with the function int()

– leoburgy
Nov 21 '18 at 13:19













are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

– Avishay Cohen
Nov 21 '18 at 14:29







are you using python3 or python2 ? like @Adelin mentiond below, they have different output depending on the python version so isdigit() might not be a good fit here. also

– Avishay Cohen
Nov 21 '18 at 14:29














1 Answer
1






active

oldest

votes


















-1














You were breaking out of loop when user inserted incorrect value (not digit) instead of letting loop go on to ask him again, you should have break the loop when user typed correct value.



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
break
else:
print('A number must be entered')

#print('Please enter age')
used_variable = int(age)
if used_variable > 18:
print('user is older than 18')





share|improve this answer





















  • 1





    so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

    – Adelin
    Nov 21 '18 at 13:14











  • I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

    – Filip Młynarski
    Nov 21 '18 at 13:15











  • If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

    – Adelin
    Nov 21 '18 at 13:29











  • If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

    – Filip Młynarski
    Nov 21 '18 at 13:32











  • Thanks for answer, it worked.

    – Jacob
    Nov 22 '18 at 12:12











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412796%2fpython-using-if-loop-to-make-sure-user-input-is-a-number-then-manipulate-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









-1














You were breaking out of loop when user inserted incorrect value (not digit) instead of letting loop go on to ask him again, you should have break the loop when user typed correct value.



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
break
else:
print('A number must be entered')

#print('Please enter age')
used_variable = int(age)
if used_variable > 18:
print('user is older than 18')





share|improve this answer





















  • 1





    so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

    – Adelin
    Nov 21 '18 at 13:14











  • I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

    – Filip Młynarski
    Nov 21 '18 at 13:15











  • If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

    – Adelin
    Nov 21 '18 at 13:29











  • If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

    – Filip Młynarski
    Nov 21 '18 at 13:32











  • Thanks for answer, it worked.

    – Jacob
    Nov 22 '18 at 12:12
















-1














You were breaking out of loop when user inserted incorrect value (not digit) instead of letting loop go on to ask him again, you should have break the loop when user typed correct value.



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
break
else:
print('A number must be entered')

#print('Please enter age')
used_variable = int(age)
if used_variable > 18:
print('user is older than 18')





share|improve this answer





















  • 1





    so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

    – Adelin
    Nov 21 '18 at 13:14











  • I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

    – Filip Młynarski
    Nov 21 '18 at 13:15











  • If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

    – Adelin
    Nov 21 '18 at 13:29











  • If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

    – Filip Młynarski
    Nov 21 '18 at 13:32











  • Thanks for answer, it worked.

    – Jacob
    Nov 22 '18 at 12:12














-1












-1








-1







You were breaking out of loop when user inserted incorrect value (not digit) instead of letting loop go on to ask him again, you should have break the loop when user typed correct value.



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
break
else:
print('A number must be entered')

#print('Please enter age')
used_variable = int(age)
if used_variable > 18:
print('user is older than 18')





share|improve this answer















You were breaking out of loop when user inserted incorrect value (not digit) instead of letting loop go on to ask him again, you should have break the loop when user typed correct value.



while True:
age = input('Enter age: ')
if age.isdigit():
print('Thank you, number entered is:', age)
break
else:
print('A number must be entered')

#print('Please enter age')
used_variable = int(age)
if used_variable > 18:
print('user is older than 18')






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 13:14

























answered Nov 21 '18 at 13:12









Filip MłynarskiFilip Młynarski

1,5881311




1,5881311








  • 1





    so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

    – Adelin
    Nov 21 '18 at 13:14











  • I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

    – Filip Młynarski
    Nov 21 '18 at 13:15











  • If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

    – Adelin
    Nov 21 '18 at 13:29











  • If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

    – Filip Młynarski
    Nov 21 '18 at 13:32











  • Thanks for answer, it worked.

    – Jacob
    Nov 22 '18 at 12:12














  • 1





    so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

    – Adelin
    Nov 21 '18 at 13:14











  • I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

    – Filip Młynarski
    Nov 21 '18 at 13:15











  • If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

    – Adelin
    Nov 21 '18 at 13:29











  • If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

    – Filip Młynarski
    Nov 21 '18 at 13:32











  • Thanks for answer, it worked.

    – Jacob
    Nov 22 '18 at 12:12








1




1





so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

– Adelin
Nov 21 '18 at 13:14





so, for an 'int' that does a conversion, you copy pasted an answer, with no explanation whatsoever, an answer that was already given countless of times? Yeah, thats a -1

– Adelin
Nov 21 '18 at 13:14













I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

– Filip Młynarski
Nov 21 '18 at 13:15





I did not copy pasted any answer, and I think its ok to first post code to let author see and create explanation in meantime.

– Filip Młynarski
Nov 21 '18 at 13:15













If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

– Adelin
Nov 21 '18 at 13:29





If you think this answer is a good answer that was not already posted before, then keep it, but I stick with my downvote

– Adelin
Nov 21 '18 at 13:29













If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

– Filip Młynarski
Nov 21 '18 at 13:32





If author's code only problem would be about converting input to integer you would be right, but there's other bugs that I shown and explained.

– Filip Młynarski
Nov 21 '18 at 13:32













Thanks for answer, it worked.

– Jacob
Nov 22 '18 at 12:12





Thanks for answer, it worked.

– Jacob
Nov 22 '18 at 12:12


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412796%2fpython-using-if-loop-to-make-sure-user-input-is-a-number-then-manipulate-number%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo