Home > Enterprise >  SyntaxError: invalid syntax, Location of the mistakes
SyntaxError: invalid syntax, Location of the mistakes

Time:10-30



import random
num=randint(1,10)
for x in num:
    x=int(input ('Guess The number (1-10): ')
    if x>num:
          print ('Sorry Too much!')
          continue
    elif x<num:
          print ('Sorry Too Little!')
          continue
    elif x>10:
          print ('Please Be Serious!')
          continue
    elif x==num:
          print ('Nice!!')


Excuse me, could you tell me where i'm wrong? I run this code and i always got

SyntaxError: invalid syntax

. Thanks!

CodePudding user response:

the error is in line 4, you forgot to finish the brackets there x=int(input ('Guess The number (1-10): ')) is what it should be. the issue here, is that you cant iterate through a number, so you would have to use a while loop here. you also don't need the continue statements, as this statement will restart when a condition is met. to optimise your code, this would be best:

from random import randint
num = randint(1, 10)
guess = 0
while guess != num:
    guess = int(input("Guess the number (1-10): "))
    if guess>num:
        print("too big")    
    elif guess<num:
        print("too small")
    elif guess>10:
        print("number is out of range")
print("you win!")
  • Related