Python-sys module, exception

Source: Internet
Author: User
Tags readfile stdin

Exercise 1: Title: Give a positive integer not more than 5 digits, requirements: First, it is a number of digits, second, in reverse order to print out the figures.

#encoding =utf-8

While True:

Try

Num=int (raw_input ("input a number not more than 5 digits:"))

Except

"Plese input again:"

Else

If Len (num) <=5:

Break

Print len (num)

Print str (num) [::-1]

Exercise 2: Please enter the first letter of the day of the week to determine the day of the week, if the first letter, then continue to judge the second letter.

#encoding =utf-8

week={' m ': ' Monday ', ' tu ': ' Tuesday ', ' W ': ' Wednesday ', ' th ': ' Thursday ', ' F ': ' Friday ', ' sa ': ' Saturday ', ' s ': ' Sunday '}

Prefix=raw_input ("Input the first letter:")

If Week.has_key (Prefix.lower ()):

Print Week[prefix.lower ()]

Else

Prefix2=raw_input ("Input The second letter:")

If Week.has_key (Prefix.lower () +prefix2.lower ()):

Print Week[prefix.lower () +prefix2.lower ()]

Exercise 3: Two 3-row, 3-column matrices that add data to their corresponding positions and return a new matrix:

X = [[12,7,3],
[4, 5,6],
[7, 8,9]]

Y = [[5,8,1],
[6,7,3],
[4,5,9]]

#encoding =utf-8

x = [[12,7,3],

[4, 5,6],

[7, 8,9]]

y = [[5,8,1],

[6,7,3],

[4,5,9]]

z = [[0,0,0],

[0,0,0],

[0,0,0]]

#print x, y

For I in range (len (x)):

Print "I:", I

For j in Range (3):

Print "J:", J

Print "x[%s][%s]:%s"% (I,j,x[i][j])

Print "y[%s][%s]:%s"% (I,j,y[i][j])

Z[I][J]=X[I][J]+Y[I][J]

Print "z[%s][%s]:%s"% (I,j,z[i][j])

Print Z

C:\python27\scripts>python task_test.py

i:0

j:0

X[0][0]:12

Y[0][0]:5

Z[0][0]:17

J:1

X[0][1]:7

Y[0][1]:8

Z[0][1]:15

J:2

X[0][2]:3

Y[0][2]:1

Z[0][2]:4

I:1

j:0

X[1][0]:4

Y[1][0]:6

Z[1][0]:10

J:1

X[1][1]:5

Y[1][1]:7

Z[1][1]:12

J:2

X[1][2]:6

Y[1][2]:3

Z[1][2]:9

I:2

j:0

X[2][0]:7

Y[2][0]:4

Z[2][0]:11

J:1

X[2][1]:8

Y[2][1]:5

Z[2][1]:13

J:2

X[2][2]:9

Y[2][2]:9

Z[2][2]:18

[[17, 15, 4], [10, 12, 9], [11, 13, 18]]

One line is done:

>>> [[X[i][j]+y[i][j] for I in range (3)] for J in Range (3)]

[[17, 10, 11], [15, 12, 13], [4, 9, 18]]

SYS.ARGV practice to count the number of letters in all input parameters.

#encoding =utf-8

Import Sys

Print "The command line arguments is:"

list=sys.argv

Num=0

Print List

For i in List[1:]:

if (i>= ' a ' and i<= ' Z ') or (i>= ' a ' and i<= ' Z '):

Num+=1

Print num

C:\python27\scripts>python task_test.py A B c D

The command line arguments is:

[' task_test.py ', ' A ', ' B ', ' C ', ' d ']

4

The second question: The parameters you enter are all numbers, calculating the cumulative sum of all the numeric parameters

#encoding =utf-8

Import Sys

Print "The command line arguments is:"

list=sys.argv

Sum=0

Print List

For i in List[1:]:

Sum+=float (i)

print "sum:", sum

C:\python27\scripts>python task_test.py 1 1 1.1

The command line arguments is:

[' task_test.py ', ' 1 ', ' 1 ', ' 1.1 ']

sum:3.1

According to Wu Old idea:

#encoding =utf-8

Import Sys

If Len (sys.argv[1:]) >=1:

Sum=0

Else

sum = None

For i in Sys.argv[1:]:

Try

Sum+=float (i)

Except

continue# here means that there is no place where the numbers are not wrong,

Print sum

#encoding =utf-8

Import Sys

Import OS

def readfile (filename):

F=open (filename)

While True:

Line=f.readline ()

If Len (Line.strip ()) ==0:

Break

Print Line

F.close ()

Print "sys.argv[0]--------", sys.argv[0]

Print "sys.argv[1]--------", sys.argv[1]

Print "sys.argv[2]--------", sys.argv[2]

If Len (SYS.ARGV) <2:

Print "No action specified."

Sys.exit ()

If Sys.argv[1].startswith ('--'):

option = sys.argv[1][2:]

if option = = ' Version ':

Print "Version 1.2"

elif option = = ' help ':

Print "" "

This program .....

--version:prints the version number

--help:display this Help "" "

Else

print "Unknow option"

Sys.exit ()

Else

For file in sys.argv[1:]:

#print file

ReadFile (file)

Sys.stdin.readline () = = Raw_input ()

>>> Raw_input ()

Sd

' SD '

>>> Sys.stdin.readline ()

Sadf

' Sadf\n '

>>>

#coding =utf-8

Import OS

Import Sys

Counter=1

While True:

Line=sys.stdin.readline ()

If not Line.strip ():

Break

Print "%s:%s"% (counter,line)

Counter +=1

>>> Import Sys

>>> sys.version

' 2.7.14 (v2.7.14:84471935ed, Sep, 20:25:58) [MSC v.1500-bit (AMD64)] '

>>>

>>> sys.stdout.write ("sdfsdffffffffff\n")

Sdfsdffffffffff

#coding =utf-8

Import OS

Import Sys

For I in range (3):

Sys.stdout.write ("Gloryroad")

print ' \ n ', ' _ ' *60, ' \ n '

For I in range (3):

Sys.stderr.write ("Error Error")

Output the standard to the screen instead of to the file

>>> Import OS

>>> Import Sys

>>> Sys.stdout=open ("D:\\a.txt", "W")

>>> print "Love You"

>>> "Love You"

>>> FP

>>> print "Hello"

>>> Stdout.flush ()

Traceback (most recent):

File "<stdin>", line 1, in <module>

Nameerror:name ' stdout ' is not defined

>>> print ""

>>> Sys.stdout.flush ()

>>> Print

Python-sys module, exception

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.