Validation for int(input()) python

2024/10/5 14:49:21
def is_digit(x):if type(x) == int:return Trueelse:return Falsedef main():shape_opt = input('Enter input >> ')while not is_digit(shape_opt):shape_opt = input('Enter input >> ')else:print('it work')if __name__ == '__main__':main()

So when the user input a value that is not an integer, the system will repeat the input(). Else, it does something else. But it won't work, may I know why?

Answer

Check this. Input always returns a string. So isdigit() is better to use here. It returns True if all characters in a string are digits and False otherwise.

return x.isdigit() will evaluate to True/False accordingly, which will be returned

def is_digit(x):return x.isdigit()def main():shape_opt = input('Enter input >> ')while not is_digit(shape_opt):shape_opt = input('Enter input >> ')else:print('it work')    if __name__ == '__main__':main()
https://en.xdnf.cn/q/119690.html

Related Q&A

How to get data out of a def function in python [duplicate]

This question already has answers here:How do I get ("return") a result (output) from a function? How can I use the result later?(4 answers)Closed 1 year ago.Trying to simplify lots of repe…

Calculating RSI in Python

I am trying to calculate RSI on a dataframedf = pd.DataFrame({"Close": [100,101,102,103,104,105,106,105,103,102,103,104,103,105,106,107,108,106,105,107,109]})df["Change"] = df["…

Pandas groupwise percentage

How can I calculate a group-wise percentage in pandas?similar to Pandas: .groupby().size() and percentages or Pandas Very Simple Percent of total size from Group by I want to calculate the percentage…

How to deal with large json files (flattening it to tsv) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 3 years ago.Improve…

How can I find max number among numbers in this code?

class student(object):def student(self):self.name=input("enter name:")self.stno=int(input("enter stno:"))self.score=int(input("enter score:"))def dis(self):print("nam…

Assert data type of the values of a dict when they are in a list

How can I assert the values of my dict when they are in a list My_dict = {chr7: [127479365, 127480532], chr8: [127474697, 127475864], chr9: [127480532, 127481699]}The code to assert this assert all(isi…

Loading tiff images in fiftyone using ipynp

I am trying to load tiff images using fiftyone and python in ipynb notebook, but it just doesnt work. Anyone knows how to do it?

Regular expression to match the word but not the word inside other strings

I have a rich text like Sample text for testing:<a href="http://www.baidu.com" title="leoshi">leoshi</a>leoshi for details balala... Welcome to RegExr v2.1 by gskinner.c…

Make one image out of avatar and frame in Python with Pillow

If I haveandneed to getdef create_avatar(username):avatar, frame, avatar_id = get_avatar(username)if avatar is not None and frame is not None:try:image = Image.new("RGBA", size)image.putalpha…

Could not broadcast input array from shape (1285) into shape (1285, 5334)

Im trying to follow some example code provided in the documentation for np.linalg.svd in order to compare term and document similarities following an SVD on a TDM matrix. Heres what Ive got:results_t =…