豆瓣
扫码直接下载
在读 深入浅出程序设计
√把input转换为int型(通过input给变量赋值,然后再对变量整数化):print('Welcome!') g = input('Guess the number: ') guess = int(g) if guess == 5: print('You win!') else: print('You lose!') print('Game over!')引自 starting to codeThe editor saves the code you write into a file on your hard disk.The code(sometimes called the source code) is just text, and it can be written and read by humans.But computers can't process text meant for humans, at least not very well.That's why we need a tool to translate the human-friendly source code into the binary 1s and 0s that computers do understand.That's what an interpreter does.In this book,an interpreter called Python is used.引自 starting to codeFortunately,Python3 comes with a built-in application called IDLE,which does both jobs and more.IDLE enables you to write and edit Python code,it translates this code into binary form, and finally, it runs the Python3 program.Because of this,IDLE is known as an Integrated Development Environment.引自 starting to codeA path refers to the set of instructions that the computer will actually follow(or excute). Your code is like a street network, with lots of sections of code connected together just like the streets in a city.When you drive through a city, you make decisions as to which streets you drive down by turning left or right at different intersections.It's the same for a program.It also needs to make decisions from time to time as to which path to take, but for your code,it is like driving along a road, it's executing a particular path.引自 starting to codeYour program makes a decision using a branch condition.A branch condition has the value true or false.If the branch condition is true,it runs the code on the branch.And if the branch condition is false,it runs the code on the false branch.引自 starting to codePython uses indents to connect a sequence of commands together to form paths.So how you connect branches together?You simply indent the second branch in by one more level.(Intends matter in python.Be careful how you indent code in Python;if you don't indent your code correctly,your code might do something wildly different from what you expect.).By default IDLE idents using 4 SPACES.If you press TAB,IDLE converts it into SPACE characters.引自 starting to codeLoops are a little like branches.Just like branches, loops have a condition(the loop condition)that is either true or false.Also,like the if part of branches,if the loop condition is true, then a loop will run a given piece of code.For a branch,this code is called the body.For a loop,it's called the loop body.The big difference between a loop and a branch is how many times it runs the code associated with it.A branch will run its code only once.But a loop will run the loop body,then check the loop condition again and,if it's true,it will run the loop body again.And again.In fact,it will keep running the loop body until the loop condition becomes false.引自 starting to codefrom random import randint secret=randint(1,10) print('Welcome!') guess=0 while guess!=secret: g=input('Guess the number:') guess=int(g) if guess==secret: print('You win!') else: if guess >secret: print('Too high!') else: print('Too low!') print('Game over!')引自 starting to codeProgramming Tools: *Programs are created from code statements: commands do things. branches decide things. loops repeat things. *Conditionals help you decide if something is True or False. *Assignment sets a name to a value. *A named value is stored in a “variable”. Python Tools: *if/else branches *while loops *= assignment operator *== equality operator *!= inequality operator *> greater than operator *print() displays a message on screen *input() gets and returns user input *int() converts characters to numbers *randint() produces a random number引自 starting to code
√把input转换为int型(通过input给变量赋值,然后再对变量整数化):
> 青青子衿的青的所有笔记(199篇)
The computer keeps track of individual characters by using two pieces of information: t...
As programs grow, the code often becomes more complex.And complex code can be hard to r...
表示其中内容是对原文的摘抄