## Python guessing game - while loop to allow only whole numbers

while loop guessing game python
modify the guessing-game program so that the user thinks of a number that the computer must guess.
python higher or lower game
python guessing game with functions
write a python program to guess a number between 1 to 9
random number game

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
```

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`:

11. Guessing Games with Random Numbers and Loops, For example the number guessing game below loops for each guess that the user There are two major types of loops in Python, for loops and while loops. It is natural to assume that range(10) would include 10, but it stops just short of it. Let her know what the number was, so she doesn't spend the rest of her life  Basic game using Python 3 that allows user to guess a random number and find out how many tries it took.

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()
```

“Guess the Number” game, This is a good game to code because it uses random numbers, loops, and input from the user in a short program. This function will come up with a random number for the user to guess. 4. The randint() function returns random integer each time, just as rolling dice you'll Loops let you execute code over and over again. #20 Python Tutorial for Beginners | While Loop in Python - Duration: 12:43. Telusko 386,878 views

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......."

```

Number guessing game in Python, random.randint(a, b): This method is used to generate random integer In the program, while loop runs until the number guessed by the user is equal to In this code, a random number is generated between 1-9 using the randint() function. After that, we can run more raw_inputs as guesses and calculate whether the guess was too high or low in a while loop with if statements. Our while loop will run the functions in it until a certain number of guesses. In this case the while loop will be set to while numberofGuesses < 8:. In the while loop, we can do the following:

While Loops, Each time an iteration is completed, the condition of the while statement is However, the do while can be imitated in Python by making sure that the while loop condition is initially true: The inner loop (loop 2) skips all the odd numbers and prints only the even numbers. Let's use a basic guessing game as an example. Guessing Game. This script is an interactive guessing game, which will ask the user to guess a number between 1 and 99. We are using the random module with the randint function to get a random number. The script also contains a while loop, which make the script run until the user guess the right number.

Build a Guessing Number Game with Python, Build a fun number guessing game using python in the terminal. the random module will let us generate a random number for the user to guess. This assigns the variable to a random integer between 1 and 50. set an if for guess​===number , which breaks the loop if true and prints out that you guessed the number in  The computer will think of a random number from 1 to 20, and ask you to guess it. The computer will tell you if each guess is too high or too low. You win if you can guess the number within six tries. This is a good game to code because it uses random numbers, loops, and input from the user in a short program.

I am trying to create a number guessing game in python 3.7 and am , This is a guessing the number game. import random # this statement will allow the options for playing and quiting the game. play = True while play: difficulty + '! You guessed the number in ' + guessesTaken + ' guesses!') if guess Okay this may sound stupid but I've only been using python since the 13th  Today we are going to make an interactive guessing game in Python. This is going to be a simple guessing game where the computer will generate a random number between 1 to 10, and the user has to guess it in 5 attempts. Based on the user’s guess computer will give various hints if the number is high or low. When the user guess matches the number computer will print the answer along with the

• 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` but that will fail on negative numbers
• Why not just convert the input to an int, `guess=int(input("guess a number"))`, that way it's always the correct type.
• @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.
• Try it out, it wouldn't except `1.00000000000000000035` or even `1.00000000000000000000000000000000000000000000000000000000000000000000000000000035` .. I'll add a screenshot to show it works.