How to check if a value is within 10 of another value using python

python, python-3.x

Solution

if heads - 10 <= int(guess) <= heads + 10:

alternatively

if abs(heads - int(guess)) <= 10:

Problem

I know similar questions have been asked but please bear with me. I'm helping my younger cousin who is creating a program in python where a coin is flipped 1000 time and the "player" so to speak guesses how many time heads appears. She then wants to check if the guess is within 10 of the number of times heads appears and respond acordingly (liar you were (answer - guess) off) or (good job only (answer - guess) off). I am used to working with UNIX and python seems to go about projects a very different way. this is the code maybe using ranges somehow to check if guess is within range? ``` #Guess how many times heads will occur when a coin is flipped 1000 time import random import time print('I will flip a coin 1000 times. Guess how many times it will come up heads. (Press enter to begin)') int(guess) = input() flips = 0 heads = 0 while flips < 1000: if random.randint(0, 1) == 1: heads = heads + 1 flips = flips + 1 if flips == 900: print('900 flips and there have been ' + str(heads) + ' heads.') time.sleep(5) if flips == 100: print('At 100 tosses, heads has come up ' + str(heads) + ' times so far.') time.sleep(2) if flips == 500: print('Half way done, and heads has come up ' + str(heads) + ' times.') time.sleep(2) print() print('Out of 1000 coin tosses, heads came up ' + str(heads) + ' times!') time.sleep(2) print('Were you close?') int(answer) = input() ```

Original source