Kevin Shane
Kevin Shane

Reputation: 45

Comparing number ranges in Python

I want to compare number ranges. My output statement produces EX. "25-32". I want to compare number range to variables I created that look something like A_Age = (0, 2),E_Age = range(25, 32),G_Age = range(48, 53) or H_Age = range(60, 100). Nothing I tried seems to work. even if I remove the parantheses or add an "-" between the numbers. this is what I have so far.

 A_Age = range(0, 2)
 B_Age = range(4, 6)
 C_Age = range(8, 12)
 D_Age = range(15, 20)
 E_Age = range(25, 32)
 F_Age = range(38, 43)
 G_Age = range(48, 53)
 H_Age = range(60, 100)


with open("output.txt", "r") as f:
gender = f.readline().split(":")[-1].strip()
age = f.readline().split(":")[-1].strip()
print(gender) # Male
print(age) # 25-32


if 25>= age >=32:
    print("This person is between the ages of 25 and 32...")

Upvotes: 1

Views: 2636

Answers (1)

Prune
Prune

Reputation: 77880

There is no built-in range comparison in Python. You haven't described how you want this to work. However, for starters, your Boolean expression is trivially False:

if 25>= age >=32:
    print("This person is between the ages of 25 and 32...")

Can you give me an example of a number that is both less than 25 and greater than 32? Try the straightforward:

if 25 <= age <= 32:

Or simply

if age in range(25, 32+1):

Remember that a range does not include its second endpoint.

Upvotes: 1

Related Questions