Ask coding questions

← Back to all posts
I seem to have a problem i dont know how to fix with my program

It seems to be with the last 3 lines but there's no spelling errors or anything.
Its going to be a guess the number game but i'm stuck with this error.

Answered by heyitsmarcus (350) [earned 5 cycles]
View Answer
heyitsmarcus (350)

Hey @JJ_graham,

You do have several errors here:

  • You've got to make sure that your indentation always stays the same with Python. Be sure that if you're using two spaces for indentation, that you continue to use two spaces for the entirety of the script. Python won't let you indent differently each time you need to indent.
  • The incomplete conditional that @niorg2606 mentioned is also causing the script to error.
  • The _ variable is perfectly valid in Python.
  • On these lines you also have some major errors:
{"number": random.randint(1, 10)  
  ans = input()
if ans1 == "number":

I have to assume you want number to be a variable which is a random integer between 1 and 10. So, you need to set number like so:

number = random.randint(1, 10)

And then for your ans you need to wrap it in an int() method to compare the two or convert the number to a string using the str() method. I would suggest the latter (using str) as it won't break the script if the user inputs something other than a number, like so:

ans = input()
  #and ensure that ans matches the input (there is no ans1)
  if ans == str(number):

I've got the code stored into a repl that you can check out here:

If this solved your problem, please consider upvoting and marking my answer as the accepted answer to close this question. I will continue to answer any questions you might have.

SixBeeps (5221)

A few things here:

  • You have a space before import time, which Python doesn't like
  • You have an incomplete conditional (if statement) at the end, preventing compilation from completing
  • I'm no expert in Python, but I don't think '_' is a valid variable name.