第三章 猜数字游戏
共有六次机会猜一个数字,每次根据给出的结果会返回猜大了,还是猜小了.
3.1 猜数字的运行示例
3.2 猜数字程序的源代码
# This is a Guess the Number game.
import random
guessesTaken = 0
print('Hello! What is your name ?)
my_Name = input()
# 生成一个1-20之间的一个随机数
number = random.randint(1,20)
print('well, ' + my_Name + ', I am thinking of a number bewteen 1 and 20.')
for i in range(6)
print('Take a guess.')# Four space in front of "printf"
guess = input()
guess = int(guess)
if guess < number:
print('Your guess is too low.') # Eight spaces in front of "P
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
# 我认为这里应该是 i+1
guessesTaken = str(i)
print('Good job,' + my_Name +'! You guessed my number in ' + guessesTaken + 'guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number +'.')
补充一个函数range()的小知识
range(6)的结果是[0,1,2,3,4,5]
所以我认为这里给出的源代码有一处错误.
3.3 总结
其实书中还有很多对这里的细致讲解,如果你学过C语言或者一门其他的编程语言,这里的内容应该能够轻松地理解.