Skip to content
Advertisement

Don’t have idea why those errors appears

Everything in this code looks right to me, but when i put some numbers that length is different than 2 or put a word, at the end of code an error appear right below “Good bye”

How do i fix that?

Thank you in advance.

Code:

def main():
    def inputnumber(message):
        while True:
            try:
                inputnumber = int(input(message))
            except ValueError:
                print("No,no,no! Put a valid number!n")
                continue
            if len(str(inputnumber)) < 2:
                print("Hey! That's too short! Please, type a two digit number.n")
                return main()
            elif len(str(inputnumber)) > 2:
                print("Hey! That's too long! Please, type a two digit number.n")
                return main()
            else:
                return inputnumber
                break

    def yes_no(message):
        userinput = str(input(message)).lower()
        if userinput == "yes" :
            return 1
        elif userinput == "no" :
            return 0
        else:
            return yes_no("Please, use 'yes' or 'no'.")


    number = str(inputnumber("Type a two digit number: "))
    total = int(number[0]) + int(number[1])
    print(f"{total}n")

    answer = yes_no("Do you want to try again?n")
    if answer == 1:
        print("Okay! Don't forget the rules!n")
        main()
    elif answer == 0:
        print("Good bye!")
main()

Advertisement

Answer

Your inputnumber function doesn’t always return two digits (sometimes it returns None). Since it already runs everything in a while True loop, when the user enters the wrong input you should use this loop to re-prompt rather than starting over by calling main() (which will return None).

In general, your program should be using while loops consistently when it needs to potentially do something more than once. There are other opportunities for simplification — for example, there’s no point in having inputnumber convert the input to an int if the caller is immediately going to convert it back to a str.

def main() -> None:
    def inputnumber() -> str:
        while True:
            inputnumber = input("Type a two digit number: ")
            if not inputnumber.isdigit():
                print("No,no,no! Put a valid number!n")
            elif len(inputnumber) < 2:
                print("Hey! That's too short! Please, type a two digit number.n")
            elif len(inputnumber) > 2:
                print("Hey! That's too long! Please, type a two digit number.n")
            else:
                return inputnumber

    def yes_no(message: str) -> bool:
        while True:
            userinput = input(message).lower()
            if userinput == "yes":
                return True
            elif userinput == "no":
                return False
            else:
                print("Please, use 'yes' or 'no'.")

    while True:
        number = inputnumber()
        total = int(number[0]) + int(number[1])
        print(f"{total}n")

        if yes_no("Do you want to try again?n"):
            print("Okay! Don't forget the rules!n")
        else:
            print("Good bye!")
            return


main()
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement