Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I am trying to work on my loop which is inside a function and wanted it to execute from 1-10. I have no idea why is it not working.

n = int(input("skriv ett nummer mellan 1-10"))

def func(n , minn, maxn):

    while n < minn:

        n = int(input("Ange ett nytt nummer, detta var för lågt"))

    if maxn < n:

        n = int(input("ange ett nytt nummer, detta var för högt"))

    else:

        print(n)

    return "Detta var ett bra nummer"

a = func(n, 1, 10)

1 Answer

0 votes
by (36.8k points)
edited by

Use the below code:

def func(n , minn, maxn):

    while True:

        if n < minn:

            n = int(input("Ange ett nytt nummer, detta var för lågt"))

            continue

        elif maxn < n:

            n = int(input("ange ett nytt nummer, detta var för högt"))

            continue

        else:

            print(n)

            return "Detta var ett bra nummer"

If you are a beginner and want to know more about Python the do check out the Data Science with Python Course 

Browse Categories

...