polors2
polors2

Reputation: 37

(Python on Visual Studio Code) SyntaxError: invalid syntax

Good day! I just installed Python and VSC with little background on either and I was wondering why I couldn't execute simple code as I copy it word for word from YouTube. (Number guessing game) Currently I have

    import random
num == 0

print("Who are you?")
name = input()

print("Hi ", +name, "! Let's play!")

number = random.randint(1,20)
print("I'm thinking of a number between 1 to 20.")

while num < 6;
    print("Try me.")
    guess = input()
    guess = int(guess)

    numberofguesses = numberofguesses + 1

if guess < number:
    print("Number is too low")
if guess > number:
    print("Number is too high")
if guess == number;
    break
if guess == number:
    numberofguesses = str(numberofguesses)
    print("Well done ", +name, "! You guessed the number in " 

+numberofguesses)
if guess != number:
    number = str(number)

and the error I'm getting is

  File "c:\dir\test.py", line 12
while num < 6
            ^

SyntaxError: invalid syntax

I get this error running python from CMD as well as using the built-in debugger of VSC.

Upvotes: 0

Views: 4416

Answers (1)

CodeCupboard
CodeCupboard

Reputation: 1585

As EdChum states you need to use colon not semi-colon.

Also you don't want to indent the import statement at the beginning.

please check this code closely against yours there are a few errors

import random
num = 0
numberofguesses = 0

print("Who are you?")
name = input()

print("Hi " + name + "! Let's play!")

number = random.randint(1,20)
print("I'm thinking of a number between 1 to 20.")

while num < 6:
    print("Try me.")
    guess = input()
    guess = int(guess)

    numberofguesses = numberofguesses + 1

    if guess < number:
        print("Number is too low")
    if guess > number:
        print("Number is too high")
    if guess == number:
        break
if guess == number:
    numberofguesses = str(numberofguesses)
    print("Well done " + name + "! You guessed the number in " 

+numberofguesses)
if guess != number:
    number = str(number)

Upvotes: 2

Related Questions