Click here to Skip to main content
15,902,299 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
guessesstaken=0;
this is showing indentation error : unxepected error

What I have tried:

Python


import random

  guessesTaken = 0

  print('Hello! What is your name?')

  myName = input()

  number = random.randint(1, 20)

 print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
 while guessesTaken < 6:
     print('Take a guess.') # There are four spaces in front of print.
     guess = input()
     guess = int(guess)
     guessesTaken = guessesTaken + 1
     if guess < number:
         print('Your guess is too low.') # There are eight spaces in front of print.
     if guess > number:
         print('Your guess is too high.')
     if guess == number:
         break

 if guess == number:

     guessesTaken = str(guessesTaken)

     print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')


 if guess != number:

     number = str(number)

     print('Nope. The number I was thinking of was ' + number)
Posted
Updated 25-Sep-20 20:15pm

Quote:
this is showing indentation error : unxepected error

With Python, indentation is part of the language. You have rules to respext.
Try this:
Python
import random

guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)

print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
    print('Take a guess.') # There are four spaces in front of print.
    guess = input()
    guess = int(guess)
    guessesTaken = guessesTaken + 1
    if guess < number:
        print('Your guess is too low.') # There are eight spaces in front of print.
    if guess > number:
        print('Your guess is too high.')
    if guess == number:
        break

if guess == number:
    guessesTaken = str(guessesTaken)
    print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
    number = str(number)
    print('Nope. The number I was thinking of was ' + number)
 
Share this answer
 
v3
Comments
Maciej Los 26-Sep-20 3:35am    
5ed!
Patrice T 26-Sep-20 5:15am    
Thank you
Indentation in Python is extremely important - it controls the execution flow.
Start by "pulling" the initial stuff over to the left:
Python
import random

  guessesTaken = 0
Becomes:
Python
import random

guessesTaken = 0
And so on.
Then check that you haven;t mixed tabs and spaces in your other indents: Python treats them both as a single character whitespace, but the visually appear to be the same:
Python
# Look at these:
    a = 1
	b = 2
The first line has four spaces, the second has one tab character. Python considers them to be different indentation!
Check your editor converts tabs to spaces!
 
Share this answer
 
Comments
Maciej Los 26-Sep-20 3:36am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900