Python third week (2): summing the number of columns, temperature conversion exception handling, chickens and rabbits with cage

Number Summation

A given number A ( . 1 A≤ . 9) and a non-negative integer n-( 0 ≦ N ≤ . 1 0 0 0 0 0), find the number of columns and the sum SUM = A + A A + A A A + + A A A ( n-number a).

For example, A = 2,  n- = time. 3, SUM = 2 + 2 2 + 2 2 2 = 2 . 4 . 6.

 

a=int(input())
b=int(input())
n=0
sum=0
for i in range (1,b+1):
    n = (n * 10)+a
    I = I + n
 print (I)

The board put meters

According to legend, the ancient Indian king Scotia rare praise to reward his smart and capable prime minister of according to Seoul (chess inventor), and asked what he needed, according to Seoul reached replied: "As long as the king in chess board first put the grid grain of wheat, two second place the grid, the third grid to have placed four, since the proportion of each Giga Click double grid 64 has been placed (a chess board frame 8 × 8 = 64) I would of gratitude, the other I do not want anything, "King thought:"! this is how much, is not easy to "let people carry a bag of wheat, but all with less than a minute gone, come back a bag of it fast and not a result of run out of food all over India were not enough, the King wondered, how may lose track of this account, you help calculate king, totaling the number of grains of wheat, to write the program.

 

n=1
m=0
for i  in range(1,65):
    n=pow(2,i-1)
    m=m+n
print(m)

 

Translation exception handling temperature

 

Temperature portray two different systems: degrees Celsius (Celsius) and Fahrenheit (Fabrenheit).

 

Please write programs to convert Fahrenheit to Celsius user input, or input conversion Celsius to Fahrenheit.

 

Conversion algorithm is as follows: (C indicates Celsius, F represents F)

 

         C = ( F - 32 ) / 1.8‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

 

         F = C * 1.8 + 32‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬

 

Requirements are as follows:

 

(1) using the input and output ends degrees C uppercase or lowercase letters C, the temperature may be an integer or decimal, such as: 12.34C refers to degrees Celsius 12.34;

 

F (2) using the input and output ends of the small print uppercase letter F or F, the temperature may be an integer or decimal, such as: 87.65F refers to 87.65 degrees Fahrenheit;

 

Question (3) considered abnormal input, such as input sub-rule throw an exception;

 

(4) using the input () to get the test case input, not to increase the prompt string.

try:
    a=input()
    if a [-1] in ['c','C']:
        f=1.8*eval(a[0:-1])+32
        print("{:.2f}F".format(f))
    elif a [-1] in ['f','F']:
        C = (the eval (A [0: -1]) - 32) /1.8
         Print ( " {:} .2f C " .format (C))
     the else :
         Print ( " input error, only the last one is a 'C' , 'c', 'F', 'f' " )
 the except NameError:
     Print ( " attempt to access the variable name does not exist " )
 the except SyntaxError:
     Print ( ' syntax error ' )

Chickens and rabbits with cage

A cage off a number of chickens and rabbits (chicken has two legs, rabbit has four legs, no exceptions).

Already know the total number of foot cage is a, then the cage at least the number of animals up to how many animals? 

 

Input Format

 

Line 1 enter a positive integer n (n≤1000), n represents the number of test data sets, then n sets each test line, each line a positive integer a (a <32768). Tip: Enter using the input (), do not add extra message.

 

Output Format

 

Output n rows, each row corresponds to one input, comprising two positive integers, the first one is the minimum number of animals, the second largest number of animals, and a space between the two positive integers. Output n rows, each row corresponds to one input, comprising two positive integers, the first one is the minimum number of animals, the second largest number of animals, and a space between the two positive integers. If the answer does not meet the requirements, the two outputs 0.

N=int(input())
for i in range (N):
    n=int(input())
    if n % 2 != 0:
        of a = I = 0
     Elif N% 4 == 0:
        a = n / 2
        i = n / 4
    else:
        i = (n / 2)/4+1
        a = n / 2
    print ("%d %d"%(i,a))

 

Guess you like

Origin www.cnblogs.com/linjiaxin59/p/12518997.html