2

I'm trying to check input for a few conditions then converting the string into an integer, after this I want to make sure the integer is not negative otherwise prompt the user to input again.

it works with the string conditions , but when I type in a negative number input throws an error "input expected at most 1 arguments, got 2"

any ideas on how to evaluate this ?

     #This compares whether the bet placed is greater than the value in the players chip_balance. It prompts the player for a lower bet if it is of greater value than chip_balance
 while bet > chip_balance:
      print('Sorry, you may only bet what you have 0 -', chip_balance)
      bet = input("Place your bet: ")
      while bet == '':
          bet = input("Can't be an empty bet please try again ")
      while bet.isalpha() or bet == '':
          bet = input("Must be a numerical value entered \n \n Place You're bet:")

      bet = int(bet)
 if bet < 0:
      bet = input("Sorry, you may only bet what you have sir! 0 \-", chip_balance)

      bet = int(bet)
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69

2 Answers2

2

sh4dowb already identified your error - you are giving 2 parameters to to input([prompt]) - which only accepts 1 text input as prompt.


Beside that, there is lots of room for improvement:

You could use error handling with validation. Also string formating comes in handy:

Derive two custom errors from ValueError - we raise them if validation does not work out.

class ValueTooLowError(ValueError):
    """Custom error. Raised on input if value too low."""
    pass

class ValueTooHighError(ValueError):
    """Custom error. Raised in input if value too high."""
    pass

Code:

chip_balance = 22

while True:
    bet = input("Place your bet  (0-{}): ".format(chip_balance))

    try:
        bet = int(bet) # raises a ValueError if not convertable to int

        # bet is an int, now we do custom validation
        if bet < 0: 
            raise ValueTooLowError()
        elif bet > chip_balance: 
            raise ValueTooHighError()

        # bet is fine, break from the `while True:` loop
        break          

    except ValueTooLowError:
        print("Can not bet negative amounts of '{}'.".format(bet))

    except ValueTooHighError:
        print("You do not own that much credit! You got {}.".format(chip_balance))       

    except ValueError as e:
        if bet == "":
            print("You are the silent type? Be sensible: input a number.")
        else:
            print("Try again.")

print("You betted: {}".format(bet))

Output (lines spaced):

Place your bet  (0-22): -30
Can not bet negative amounts of '-30'.

Place your bet  (0-22): 55
You do not own that much credit! You got 22.

Place your bet  (0-22): # nothing
You are the silent type? Be sensible: input a number.

Place your bet  (0-22): 9  
You betted: 9

Suggested read:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1
bet = input("Sorry, you may only bet what you have sir! 0 \-", chip_balance)

input function does not take 2 parameters, while print does. You can do it like this;

bet = input("Sorry, you may only bet what you have sir! 0 \- {}".format(chip_balance))
cagri
  • 807
  • 8
  • 17