Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
171 views
in Technique[技术] by (71.8m points)

python - 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!
")
                continue
            if len(str(inputnumber)) < 2:
                print("Hey! That's too short! Please, type a two digit number.
")
                return main()
            elif len(str(inputnumber)) > 2:
                print("Hey! That's too long! Please, type a two digit number.
")
                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}
")

    answer = yes_no("Do you want to try again?
")
    if answer == 1:
        print("Okay! Don't forget the rules!
")
        main()
    elif answer == 0:
        print("Good bye!")
main()
question from:https://stackoverflow.com/questions/65930115/dont-have-idea-why-those-errors-appears

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!
")
            elif len(inputnumber) < 2:
                print("Hey! That's too short! Please, type a two digit number.
")
            elif len(inputnumber) > 2:
                print("Hey! That's too long! Please, type a two digit number.
")
            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}
")

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


main()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...