SouthBySouth
SouthBySouth

Reputation: 1

program says it isn't a valid input when it is

I've run into a bug where the program seems to think that my input into an input statement isn't within the parameters, when it clearly is.

This is the code i'm having trouble with:

time.sleep(1.5)
print()
print()
print("You have chosen", horsechoice)
time.sleep(1)
print("How much do you want to bet?")
while True:
    try:
        moneyinput = int(input())
        racebegin(moneyinput)
        break
    except:
        print("Not a valid amount of money.")
        continue

Even when I input an integer, it still states that I didn't input a valid amount of money.

Upvotes: -1

Views: 74

Answers (1)

Barmar
Barmar

Reputation: 781255

If you only want to check the input validity, you should wrap the try/except only around the int(input()) call, not racebegin(). Your code is probably catching an error in racebegin().

It's also a good idea to narrow down the type of error you're catching with except:. Doing that might have prevented the problem in the original code, unless racebegin() was also raising ValueError.

while True:
    try:
        moneyinput = int(input())
        break
    except ValueError:
        print("Not a valid amount of money.")

racebegin(moneyinput)

Upvotes: 2

Related Questions