python猜数字游戏异常处理_猜数字游戏,错误。

我想你是想用

你的最后两个if条件也在你的循环之外,没有太大意义。您已经检查了if guess == number一次,如果满足该条件,则中断。if guess != number您已经通过分别使用来检查这一点。在

print = ...?print语法是print(some_stuff, ...)。假设你的问题是第一个缩进。在

而且,raw_input是python2的,它只是python3中的input。您可以使用%格式化程序或使用.format清理一些打印语句。在

修正代码:(Python3版本,因为这是问题中标记的内容…)import random

name = input("what is your name?")

number = random.randint(1, 20)

#print("%s I am thinking of a number between 1 and 20..." % name)

print(name + " I am thinking of a number between 1 and 20...")

guesses = 0

while guesses < 7:

guess = int(input("Take a guess."))

guesses += 1

if guess < number:

print ("your guess is too low!")

elif guess > number:

print ("your guess is too high!")

else:

#print("Good job %s you guessed my number in %d guesses" % (name, guesses))

print ("Good job, " + name + " you guessed my number in " + str(guesses) + " guesses!")

break

你可能感兴趣的:(python猜数字游戏异常处理)