-3

The error we are getting is that it is looping infinitely and does not appear to be picking a number to be the correct choice

import random
print ("Guess a random number between 1 and 10")
number = random.randint(1,10)
guessTaken = 0
print ("Guess!") 
guess = int( input())
while guessTaken < 6:
  guess != guess+1
  print ("Wrong!, guess again")
  if guess == input(): 
     print ("Correct")
print (  )
  • 2
    1. What do you think `guess != guess+1` is doing? 2. Why are you asking the user for a second `input()` (and without converting it to `int`)? – DeepSpace Sep 18 '18 at 14:32

4 Answers4

1

The loop's termination is based on the value of guessTaken; since that is never changed, once the loop is entered, it will never end.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
1

Your code has many mistakes but I will try my best to fix them here:

First of all guess != guess+1 serves no purpose you are checking if guess is not equal to guess+1 (it isn't) which means that this line is always returning True and you aren't event doing anything with it.

I believe you meant to write:

guessTaken += 1

Which increments the number of guesses taken by 1

Next you will need to convert the second input to an int to compare it to guess so I recommend doing:

if guess == int(input()): 

instead of

if guess == input(): 

Finally I suspect you want to exit the loop once the number has been guessed so I would add a break statement in the if condition as such:

if guess == int(input()): 
     print ("Correct")
     break
Joseph Chotard
  • 666
  • 5
  • 15
0

You have many mistakes in your code. Not sure what you need, but you can try below:

import random
print ("Guess a random number between 1 and 10")
number = random.randint(1,10)
guessTaken = 0
wrong = True
print (number) 
guess = int( input())

while guessTaken < 6: #Maximum guesses are 6
    if guess != number:
        print ("Wrong!, guess again")
        guess = int( input())
    else: 
        print ("Correct")
        break #remember to put break when found correct number
    guessTaken += 1

if guessTaken == 6:
    print ("Maximum guessed")
SonDang
  • 1,468
  • 1
  • 15
  • 21
0

I have tried to modify your code:

import random
print ("Guess a random number between 1 and 10")
number = random.randint(1,10)
guessTaken = 1

while guessTaken < 6:
    if guessTaken == 1:
        print('Guess!')
        guess = input()
    else:
        print('Guess Again')
        guess = input()

    if int(guess) == number:
        print('Correct')
        break
    else:
        print('Wrong')

    guessTaken = guessTaken + 1 #counter
SmitM
  • 1,366
  • 1
  • 8
  • 14