Use Python to write 1 plus to any number of interactive small programs about range () function refinement

Source: Internet
Author: User

Serious!?????

------------

A script that uses Python to add a number from 1 to any size (computer feel All) 交互 .

Here I used the range function (I like to call it Python ridge function, because range has "ridge" meaning), this function can have an interesting usage: range (x), the interpreter will list all the integers from 0~x-1, if it is range (y,x), the interpreter will list from y~ All integers of x-1 (x-1 is equivalent to excluding X, it should be understood); There is also a range (y,x,z), and the interpreter will jump Z lists all integers from y~x-1, the default jump spacing is 1, as range (Y,X) is equivalent to range (y,x,1).

Look at the picture should be better understood:

Note In the last sentence, range (11,2) is not equivalent to range (0,11,2). Here Range (11,2) is judged as "from 11~1" of all integers, naturally returning is an empty list, instead of None,none no practical meaning, and empty list has practical meaning.

Step into the chase.

#PlusCal V1.9 (alpha) by:b1ta - 2015/07/17/09:07name = raw_input("Eh,What is your name:")print "Hello," + name + "!"y = 1while y < 2:    a = input("Please enter a number:")    sum = 0    for i in range(1,a+1):        sum += i    print "Nice,1+2+3+..." ,a,"=",sum    y = input("If you want to try it again,Enter number 1,or enter other number to exit:")print name + ",Press your ‘Enter‘ to say Good-bye."exit()#Have fun!

This is a flawed script, I have the parameter of range in the For Loop (1,x+1), can be omitted in the previous 1, just a 0 problem.

The variable is a 不能转换 string, but can be mixed, but be aware that if the integer type must be comma (,). The following error occurs when you use the plus sign

>>> x=1
>>> y="a"
>>> print x+y

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print x+y
TypeError: unsupported operand type(s) for +: ‘int‘ and ‘str‘

Because integer types cannot be combined with strings.

There are many defects, such as the sixth line while the loop condition can be improved <y/n>, and the corresponding 11th line. The 6th line should include the "re-enter if not given condition (number)" mechanism (try).

while True:   a=None   try:       a=int(input("Please enter a number:"))   except:pass   if type(a)==int:break

If the input is not a number, it will always loop.

The double equals sign is used for detection, such as 1==2 get

False

On line 11th, if you enter a number less than 2, the calculation continues, and if not, jumps out while. There is a flaw here, and when I enter a It also loops. The reason is because the variable here is a, and it is already assigned, so naming a is not desirable, so a is changed to Num. The name is best not casually, unrelated side to the time oneself also can not understand:

name = raw_input("Eh,What is your name:")print "Hello," + name + "!"y = 1while y < 2:    while True:        num=None        try:            num=int(input("Please enter a number:"))        except:pass        if type(num)==int:break    sum = 0    for i in range(1,a+1):        sum += i    print "Nice,1+2+3+..." ,num,"=",sum    y = input("If you want to try it again,Enter number 1,or enter other number to exit:")print name + ",Press your ‘Enter‘ to say Good-bye."exit()

Now the selection of rows 6th and 15th is also judged (whether or not to continue), my method is to enter 1 to continue, and the other numbers to exit. This is good to do, change while the condition is:

while 0<y<2

Finally, modify the exit question, delete the bottom two lines, add:

raw_input("Press your ‘Enter‘ to say Good-bye.")

The exit () is because it kills the interpreter, which can be used to test the difference . For example, sometimes the script ends up with other code, and exit () "kills" the interpreter at this point (killing the process, or shutting down the interpreter).

Final completion:

#PlusCal V3.0 by:b1ta - 2015/07/17/15:06name = raw_input("Eh,What is your name:")print "Hello," + name + "!"y = 1while 0<y<2:    while True:        num=None        try:            num=int(input("Please enter a number:"))        except:pass        if type(num)==int:break    sum = 0    for i in range(1,num+1):        sum += i    print "Nice,1+2+3+..." ,num,"=",sum    y = input("If you want to try it again,Enter number 1,or enter other number to exit:")raw_input("Press your ‘Enter‘ to say Good-bye.")#Have fun!

  

╮ ( ̄▽ ̄ ") ╭

Shameless seeking recommendation: Do you have any interest in this article? (Correct answer: Yes?) (? ? ?)? )

there! No

x('? ') )? ♥ So click on the lower right side of the recommended bar ... ~\ (≧▽≦)/~It's such a happy decision! x(┳_┳) ...

I will try again ... (?﹏?)

So, to encourage me, what's the recommendation at the bottom right?? (? ? ?)?

Yes, yes !

Use Python to write 1 plus to any number of interactive small programs about range () function refinement

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.