Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error Handling is weird
#1
So, I have a try/except sequence to make sure my program doesn't get bogged by a user entering a non-number in response to a prompt asking for a number. Oddly enough, when it gets a non-number, it prints my message I have set up as the action for the "except" twice rather than once. Why is it repeating the try twice?

import random

def game (theNumber, newNumber):
    try:
        int(newNumber)
    except:
        print("That's not going to work. I need a number from 0 to 10.")
        return False, False, theNumber

    myNumber = int(newNumber)
    if myNumber >= 11 or myNumber < 0:
        print("I said 0 to 10. Let's try again:")
        return 
    elif theNumber == myNumber:
        return True, True, theNumber
    else:
        return True, False, theNumber

while True:
    
    doYou = input("Would you like to play? ")
    if doYou == "Yes":
        theNumber = random.randrange(0,10)
        newNumber = input("Guess a number from 0 to 10. ")
        game(theNumber, newNumber)
    else:
        print("Consent is vital, I need a clear answer: Yes")

    myResult = game(theNumber, newNumber)

    #print(myResult[0])
    #print(myResult[1])

    if myResult[0] == False:
        continue

    if myResult[1] == False:
        print("Nope, the answer was", theNumber)
    elif myResult[1] == True:
        print("Congratulations, you guessed it:", theNumber)
    else:
        continue
"Nobody really knows how to program, the pros Google too." -A Google Engineer
Reply
#2
You are calling game inside your if block and outside so all that code runs twice on yes and once on no:
It should instead be only called once, but let's look a little more at your input validation.  Firstly never catch a naked except.  In this case the exception you want is ValueError
Here is a look at a more elegant way to do this:
This function will loop every time it gets an invalid input, instead of just looping the whole program.

This is an organized rewrite of what you have so far.  Obviously try to learn from it what you can rather than simply copy it:  
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trouble with Average Calculation Despite Error Handling autodecay 3 1,091 Jul-02-2024, 01:53 PM
Last Post: Hichem
Shocked Script get's really weird error DaJohn 7 1,620 May-26-2024, 09:10 AM
Last Post: snippsat
  Calculating Average with Error Handling mikasa 7 3,127 May-07-2024, 07:48 AM
Last Post: snippsat
Star python exception handling handling .... with traceback mg24 3 3,366 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 7,256 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 8,862 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 4,164 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Weird function defaults error? wallgraffiti 5 3,152 Aug-07-2020, 05:55 PM
Last Post: deanhystad
  Weird SQLAchemy connection string error pawpaw 0 1,976 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  Error handling using cmd module leifeng 3 4,273 Jun-06-2020, 06:25 PM
Last Post: leifeng

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020