Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

Python Variable Range?

import time

def dotdotdot():
    time.sleep(1)
    print(". . .")
    time.sleep(1)
    print(" . . .")
    time.sleep(1)
    print("  . . .")
    time.sleep(1)
    print("   . . .")
    time.sleep(1)

print("Hello! Welcome to the Fortune Teller!")

option = int(input("What would you like me to do for you?\n([1] EXIT / [2]LUCKY NUMBER)"))
if option == 1:
    print("Thanks for comming anyways!")
    dotdotdot()
    exit()
if option == 2:
    print("Alright!")
    **mob = int(input("Please enter the month you were born in:  "))**
    **if mob.range != (1,12):**
        print("I cannot understand you... Try again!")
        time.sleep(1)
        mob = ("Please enter the month you were born in:  ")

else:
        print("Sorry I cannot understand you! Try again:  ")
        option = int(input("What would you like me to do for you?\n([1] EXIT / [2]LUCKY NUMBER)"))

MOD: Updated the question to include formatting so people can read the code more easily. Steven Parker below has already listed the links that should be helpful for this in the future. Thanks!

Steven Parker
Steven Parker
243,215 Points

Did you have a question about this code?

And please use Markdown formatting for the code. Instructions can be found in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or for more detailed instructions, you can watch this video on code formatting.

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

In your code, mob appears to be an int. The int type does not have a range method.

If you are looking to see if an int is within a range you could use

>>> mob = 14
>>> mob in range(1,12)
False
>>> mob in range(10, 14)
False
>>> mob in range(10, 15)
True

EDIT: Correction: When using in range(n,m), the full range of number is not generated. Instead, the method range.__contains__() is used that has near linear time because it calculates if the number is in the range. The is the preferred method when the range is not contiguous. That is, it has a step value other than 1.

For more information, see this StackOverflow post on in range performance. Also see performance code below.

That said, it is a bit less efficient to use in on a range of contiguous numbers, then check if an int is within that range. The more Pythonic way would be to use > and <:

>>> mob = 14
>>> mob >= 1 and mob < 12
False
>>> mob >= 10 and mob < 14
False
>>> mob >= 10 and mob < 15
True

Testing performance using the timeit function. By default, timeit uses 1_000_000 iterations. Even after a million iterations the total difference is very small.

import timeit

# plug in range
a = timeit.timeit(
    '1 <= plug < 5',
    setup='plug=3')

# plug not in range
b = timeit.timeit(
    '1 <= plug < 5',
    setup='plug=12')
# plug in range
c = timeit.timeit(
    'plug not in r',
    setup='plug=3; r=range(1, 5)')
# plug not in range
d = timeit.timeit(
    'plug not in r',
    setup='plug=12; r=range(1, 5)')

# plug in range
e = timeit.timeit(
    'r.__contains__(plug)',
    setup='plug=3; r=range(1, 5)')

# plug not in range
f = timeit.timeit(
    'r.__contains__(plug)',
    setup='plug=12; r=range(1, 5)')

print(f"gt-lt: plug in range {a},\n"
      f"gt-lt: plug not in range {b},\n"
      f"in  r: plug in range {c},\n"
      f"in  r: plug not in range {d},\n "
      f"contains: plug in range {e},\n"
      f"contains: plug not in range {f} ")

produces:

$ python num_in_range.py 
gt-lt: plug in range 0.09480750502552837,
gt-lt: plug not in range 0.054522386053577065,
in  r: plug in range 0.1077367789694108,
in  r: plug not in range 0.07507346698548645 
contains: plug in range 0.2253128889715299,
contains: plug not in range 0.1758684900123626 

Calling the __contains__ method directly is a bit slower. It is better to let in automatically call this method.

Post back if you need more help. Good luck!!