1

I'm building simple "guess number" game in python based on book "Python Programming for the Absolute Beginner: chapter 3". I can't figure out how to make this while loop part accept only whole numbers in input:

# Setting initial values
computer = random.randint(1, 100)
guess = input("Take a guess: ")
while guess != int or guess != float:
    guess = input("Use whole numbers: ")
tries = 1

The effect should be that after user types float or string program prints "Use only whole numbers".

After user types whole number it takes him out of the loop to the game:

# Guessing loop
while guess != computer:
    if guess < computer:
        print("You're bidding to low")
    else:
        print("You're bidding to high")

    guess = int(input("Take a guess: "))
    tries += 1

I tried also, but it doesn't work either:

computer = random.randint(1, 100)
guess = int(input("Take a guess: "))
while guess != int or guess != float:
    guess = input("Use whole numbers: ")
    if guess == int:
        break
tries = 1
  • 3
    You cannot compare a value for equality against a type (try `1 == int` and you will get `False`). Also, user input is always a string. For checking for integers, you can try [`isdigit`](https://www.tutorialspoint.com/python/string_isdigit.htm) but that will fail on negative numbers – roganjosh Nov 21 '18 at 16:24
  • Possible duplicate of [How to check if string input is a number?](https://stackoverflow.com/questions/5424716/how-to-check-if-string-input-is-a-number) – roganjosh Nov 21 '18 at 16:29
  • 1
    Also: https://stackoverflow.com/questions/1265665/how-can-i-check-if-a-string-represents-an-int-without-using-try-except which might actually have been a better target – roganjosh Nov 21 '18 at 16:30
  • Why not just convert the input to an int, `guess=int(input("guess a number"))`, that way it's always the correct type. – user2699 Nov 21 '18 at 16:31
  • 2
    @user2699 because that will throw `ValueError` if they enter an input with a decimal point. You can wrap in a try/except to confirm that they entered an integer. Also, that's not the point of the exercise. – roganjosh Nov 21 '18 at 16:31
  • @roganjosh, I knew there was something I was overlooking. A `try/except` block would be a good way to go here. – user2699 Nov 21 '18 at 16:44

3 Answers3

2

You can try to cast your input to float and then use is_integer() to check if the input is an integer. However, if the input contains letters then it will raise a ValueError exception which you can handle to ask for whole numbers as input.

This would work for you:

while True:
    try:
        guess = input("Use whole numbers: ")
        if float(guess).is_integer():
            guess = int(guess)
            break
    except ValueError:
        continue

print(guess)

This would also work if the input is something like 123.0.

Edited to address the comment regarding inputs like 1.000000000000000035:

Works for inputs like 1.0000000000000000000035

Abhinav Sood
  • 799
  • 6
  • 23
  • Try it out, it wouldn't except `1.00000000000000000035` or even `1.00000000000000000000000000000000000000000000000000000000000000000000000000000035` .. I'll add a screenshot to show it works. – Abhinav Sood Nov 21 '18 at 16:50
1

You can write a custom function for checking for a valid input:

def isInteger(string):
    try:
        int(string)
        return True
    except ValueError:
        return False

And modify your while loop like this:

while not isInteger(guess):
    guess = input()
DocDriven
  • 3,726
  • 6
  • 24
  • 53
1

i think this would work for you, I am using python 2.7 and not much difference with the higher version

import random

def is_integer(num):
    try:
        num= int(num)
        return num
    except:
        False

def main(prompt, retries=5, complaint="wrong guess!!! try again..."):
    computer = random.randint(1, 100)
    while retries > 0:
        my_guess = raw_input(prompt)
        if is_integer(my_guess) == computer:
            print "Bingo!!! the answer is ",computer
            break
        retries = retries - 1
        if retries > 0:
            print "{} remaining {} tries left".format(complaint, retries)
        else:
            print "You are out of tries..."
            print "Exiting......."


main("please Guess a number? ")
mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26