On 2020-12-12 15:12, Bischoop wrote: > I need to check if input number is 1-5. Whatever I try it's not > working. Here are my aproaches to the problem: https://bpa.st/H62A > > What I'm doing wrong and how I should do it?
A range is similar to a list in that it contains just the numbers listed: >>> r = range(10) >>> 2 in r True >>> 2.5 in r False >>> r = range(1, 10, 2) >>> 2 in r False >>> list(r) [1, 3, 5, 7, 9] It also doesn't automatically convert from the string inputs you're getting from the input() function: >>> s = "5" >>> s in r False >>> int(s) in r True Additionally, note that the endpoint of the range is exclusive so >>> r = range(1, 10) >>> 10 in r False >>> list(r) [1, 2, 3, 4, 5, 6, 7, 8, 9] If you want numeric-range checks, Python provides the lovely double-comparison syntax: >>> x = 5 >>> 2 < x < 10 True >>> x = 5.5 >>> 2 < x < 10 True >>> s = "5" >>> 2 < s < 10 Traceback… >>> 2 < int(s) < 10 True Hopefully this gives you the hints that you need to troubleshoot. -tkc -- https://mail.python.org/mailman/listinfo/python-list