能分两篇写不~~~
好吧~~自己默默的允许了~~噗~~~~
真的很难翻译呀,有时英语看着满顺的,怎么译成中文,怎么读怎么不顺~~~噗~~~~~~
想我当年(小学,初中~额````)也是语文课代表呀~~写得一手好字和好文呀~~~
蹉跎岁月~~~
纯理科生伤不起~~~
码农更加伤不起呀~~~快和闺蜜木有共同语言了~~~
求拯救~~~
周末约大小姐们吃饭逛街去吧~小的给您们提包~~
-----------------------------------吐槽的分割线-------------------------------------------
第四章 "猜数字"
“猜数字”游戏
我们将做一个“猜数字”的游戏。在这个游戏中,电脑会提供一个从1到20之间的随机数,请你猜数。你只有6次机会,但电脑会告诉你,你所猜测的数字大了还是小了。如果你在六次机会中就猜到了数字,你就赢了。
这是个很好的入门游戏,因为它在相当短的程序中使用了随机数,循环和用户输入。当你在写这个游戏时,你将学到如何把值转为不同的数据类型(还有你为什么需要这么做)。
由于这个程序是一个游戏,我们要把用户叫为玩家,但“user”这个词的意思不变。
运行“猜数字”的示例
Albert
Well, Albert, I am thinking of a number between 1 and 20.
Take a guess.
10
Your guess is too high.
Take a guess.
2
Your guess is too low.
Take a guess.
4
Good job, Albert! You guessed my number in 3 guesses!
正确输入下面的代码,然后点击“ File“菜单上的guess.py.按F5键来运行程序。如果现在你不是很明白这些代码,不要担心,我将一步一步来解释。
"进行保存。给它取一个文件名,类似"猜数字"的源代码
这是我们的"猜数字"游戏的源代码。当你在文件编辑器中输入这些代码时,一定要注意在某些行前面的间距。有些行前有四个或八个空格。在你输入代码后,保存文件为guess.py。您可以在文件编辑器中按F5运行程序。如果你看到错误消息,请仔细检查你所输入的代码。
重要注意事项! 对应你的Python版本,输入对应的代码。
Python 3 代码
#!/usr/bin/env python
#coding=utf-8
# This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
Python 2 代码
#!/usr/bin/env python
#coding=utf-8
# This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = raw_input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = raw_input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
我做了下完善(你可以忽略它,因为这不是这章所学的内容,请原谅猿类们改bug的欲望):
#!/usr/bin/env python
#coding=utf-8
# This is a guess the number game.
import random
guessesTaken = 0
print('Hello! What is your name?')
myName = raw_input()
number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')
while guessesTaken < 6:
print('Take a guess.') # There are four spaces in front of print.
guess = raw_input()
try:
guess = int(guess)
except:
print("error, please input a number!")
else:
guessesTaken = guessesTaken + 1
if guess < number:
print('Your guess is too low.') # There are eight spaces in front of print.
if guess > number:
print('Your guess is too high.')
if guess == number:
break
if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')
if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)
---------------------------------------------------------------------------------------
会在下一篇进行具体代码的解释~~~~~~~~~~
小的立刻去继续写~~~
在此想默默叩谢老娘昨天找人千里迢迢带来杭州的杨梅酒~~
非常好喝~
你们这是要培养酒鬼的节奏么~~
有兴趣的兄弟姐妹们,可以来家里喝两杯呀~自带下酒菜~哈哈~~~