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
Star python exception handling handling .... with traceback mg24 3 1,282 Nov-09-2022, 07:29 PM
Last Post: Gribouillis
  Help needed with a "for loop" + error handling tamiri 2 2,528 May-27-2022, 12:21 PM
Last Post: tamiri
  Handling Python Fatal Error richajain1785 7 5,917 Oct-14-2021, 01:34 PM
Last Post: Tails86
  Error Handling JarredAwesome 5 2,939 Oct-17-2020, 12:41 AM
Last Post: JarredAwesome
  Weird function defaults error? wallgraffiti 5 2,182 Aug-07-2020, 05:55 PM
Last Post: deanhystad
  Weird SQLAchemy connection string error pawpaw 0 1,515 Jun-28-2020, 10:11 AM
Last Post: pawpaw
  Error handling using cmd module leifeng 3 2,896 Jun-06-2020, 06:25 PM
Last Post: leifeng
  Excpetion Handling Getting Error Number gw1500se 4 2,392 May-29-2020, 03:07 PM
Last Post: gw1500se
  [split] Python beginner: Weird Syntax Error mnsaathvika 1 2,139 Jul-22-2019, 06:14 AM
Last Post: buran
  Warning / Error handling in python Prarthana_12 1 5,107 Feb-08-2019, 09:21 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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