美好的一天!我刚安装了几乎没有背景的Python和VSC,我想知道为什么我不能执行简单的代码,因为我会逐字逐句地复制它。 (猜数游戏)目前我有
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)
我得到的错误是
File "c:\dir\test.py", line 12
while num < 6
^
SyntaxError: invalid syntax
我从CMD运行python以及使用VSC的内置调试器时出现此错误。
答案 0 :(得分:2)
正如EdChum所说,你需要使用冒号而不是分号。
另外,您不想在开头缩进导入语句。
请仔细检查此代码是否存在一些错误
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)