0

I just started learning python 2 days ago and I am trying to make my very first bit of working code but I can't figure this out!!

I'm making a basic dice game and want to allow the user to 'bet' an amount of virtual money but want to prevent them from being able to insert something which is not an integer. This is what I came up with:

    bet = input ('How much would you like to bet this round? (It can be 0) ')
  while bet!=int or bet>gold or bet<0 :
    time.sleep (1)
    print ("\nInvalid bet amount! Try betting a different amount.")
    bet = input ('How much would you like to bet this round? (It can be 0) ') 

The issue I have (I think) is that the bet input is taken as a string and so will always not be an integer, leading to an inescapable loop of an invalid bet amount. If I try to say that the bet = int(input('How much would you like to bet this round?') however, the user could still input a letter and cause an error because I've stated the bet has to be an integer.

Any ideas how to bypass this?? Also please let me know if there's anything I could do better in the code I've shown. I'm a complete beginner and happy to learn :)

Thanks!

Biteri
  • 11
  • [Did you check this?](https://stackoverflow.com/questions/8075877/converting-string-to-int-using-try-except-in-python/8075959) – JenilDave Jul 14 '20 at 11:42
  • 1
    `int()` will try to convert the input to an integer. If not possible, it will throw an error -- an error that you can catch. If you don't want to delve into error trapping now (which might not be a good topic for your third day of learning) -- just ignore the problem for now. Trust the user (who, realistically, is you) to not enter something stupid. You really aren't at the point where such concerns are important, though it is definitely good that you are thinking about them. It shows good instinct on your part. – John Coleman Jul 14 '20 at 11:43

0 Answers0