python 很简单的例子,不知道哪里出错了
直接看代码
import randomsecret = random.randint(1, 100)guess = 0tries = 0print "AHOY! I 'm the Dread Pirate Roberts, and I have a secret! "print "It is a number from 1 to 99 . I'll give you 6 tries. "while guess != secret and tries < 6: guess = input("what's yer guess? ") if guess < secret: print "too low, ye scurvy dog! " elif guess > secret: print "too high , landlubber! " tries = tries + 1 if guess == secret: print "avast! ye got it! Found my secret, ye did! " else: print "No more guesses! Better luck next time, matey! " print "The secret number was", secret
import randomsecret = random.randint(1, 100)guess = 0tries = 0print "AHOY! I 'm the Dread Pirate Roberts, and I have a secret! "print "It is a number from 1 to 99 . I'll give you 6 tries. "while guess != secret and tries < 6: guess = input("what's yer guess? ") if guess < secret: print "too low, ye scurvy dog! " elif guess > secret: print "too high , landlubber! " tries = tries + 1 if guess == secret: print "avast! ye got it! Found my secret, ye did! "else: print "No more guesses! Better luck next time, matey! " print "The secret number was", secret
[解决办法]
phyton依赖于缩进来解释的。
[解决办法]
if guess < secret:
print "too low, ye scurvy dog! "
elif guess > secret:
print "too high , landlubber! "
缩进有问题,对齐试一下吧
[解决办法]
python不使用花括号,它严格根据缩进来排语句的优先级。
[解决办法]
要对齐下缩进!!!
[解决办法]
print "too high , landlubber! "
tries = tries + 1
这个地方的缩进,把它改为:
print "too high , landlubber! "
tries = tries + 1
应该就好了
[解决办法]
缩进问题,见楼上几位的解答!
空白在Python中是重要的。
在逻辑行首的空白(空格和制表符)用来决定逻辑行的缩进层次,从而用来决定语句的分组。
这意味着同一层次的语句必须有相同的缩进。