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()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…