samedi 8 juin 2019

How to find the range of a step for a random number

I've just started learning code and i'm writing a simple program. I'm trying to figure out how to find the range of a step that a number is within.For example i have a range of 0 to 101 with a step of 10. Let's say the number is 33, so the range of a step it's in goes from 30 to 40.

How can i find these lower and upper values or what can i use for it ?

I have a solution for this but it is very messy and i want to find a better way of doing it so that i don't have to repeat the code.Here it is

import random
number = random.randint(1, 100)
def help():
    if number in range(0, 11):
        print ("It's between 0 and 10")
    if number in range(10, 21):
        print ("It's between 10 and 20")
    if number in range(20, 31):
        print ("It's between 20 and 30")
    if number in range(30, 41):
        print ("It's between 30 and 40")
    if number in range(40, 51):
        print ("It's between 40 and 50")
    if number in range(50, 61):
        print ("It's between 50 and 60")
    if number in range(60, 71):
        print ("It's between 60 and 70")
    if number in range(70, 81):
        print ("It's between 70 and 80")
    if number in range(80, 91):
        print ("It's between 80 and 90")
    if number in range(90, 101):
        print ("It's between 90 and 100")
print (number)
help()

So i basically need to automate this code so that i would only have to pass on values to get the same output.




Aucun commentaire:

Enregistrer un commentaire