Rejecting star ratings out of range

Write code · Medium · §13.4 Cleaning up and refusing honestly

Complete the function to_star(text), which converts text, the text in a survey's star rating field, to an integer and returns it. If the integer is less than 1 or greater than 5, do not return None; raise a ValueError with raise. Do not catch the ValueError that int() raises for text that is not a number; leave it as it is.

Starter code

def to_star(text):
    # Write your code here
    pass

print(to_star("4"))
for text in ["9", "good"]:
    try:
        to_star(text)
    except ValueError:
        print(text, "rejected")

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.