0

I've written some code:

mon=10
a=1
print("You have",mon,"pounds")
bet=input("How much do you want to bet?")
if bet % 1==0:
    bet=int(bet)
else:
    print("Give me a whole number please")

But when I answer I get:

Traceback (most recent call last):
    if bet % 1==0:
TypeError: not all arguments converted during string formatting
  • 2
    `bet` is a string, not a number. You need to convert it first. When you use `%` on a string, you are formatting the string, not getting the modulo of a number. – Jeff Mercado Jun 18 '17 at 16:22

1 Answers1

3

input() returns a string.

From the docs:

If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that.

You should change your code as follows:

mon=10
a=1
print("You have",mon,"pounds")
bet=input("How much do you want to bet?")
try:
    bet=int(bet)
    print(bet)
except:
    print("Give me a whole number please")

That way the program will try to convert the user intput into an integer - and if that fails it will print "Give me a whole number please"

Mike Scotty
  • 10,530
  • 5
  • 38
  • 50