python-on: the first experiment practice

7-1 jmu-python- operator - give change

When the pay, the salesperson give users find the money. Salesperson hands of 10 yuan, 5 yuan, 1 yuan (1 yuan is assumed that the minimum unit) of several denominations, their hope as little as possible (the number of) money for the bills to the user. For example, a 17 yuan need to find a user, then the user needs a 10 yuan, a $ 5, 2 $ 1.
Instead of the user 17 or 3 1 yuan 5 yuan and two $ 1.

Input formats:
Input n, n behalf to conduct tests.
Then enter integer n rows, each row represents're looking for money.

Output formats:
in accordance with the following format output, x represents the total number to find the money, the required number of representatives of each denomination?.
x = ?*10 + ?*5 + ?*1

Note: = + around and have spaces.

Sample input:

109
17
10
3
0

Sample output:

109 = 10*10 + 1*5 + 4*1
17 = 1*10 + 1*5 + 2*1
10 = 1*10 + 0*5 + 0*1
3 = 0*10 + 0*5 + 3*1
0 = 0*10 + 0*5 + 0*1

Code:

def Jmu(a):
    x = 0
    y = 0
    z = 0
    b = a
    while True:
        if a >= 10:
            a -= 10
            x += 1
        elif a >= 5:
            a -= 5
            y += 1
        elif a > 0:
            a -= 1
            z += 1
        else:
            break

    print("%d = %d*10 + %d*5 + %d*1" % (b, x, y, z))
    # print("{} = {}*10 + {}*5 + {}*1".format(b, x, y, z))

n = eval(input())
for i in range(n):
    a = eval(input())
    Jmu(a)

7-2 jmu-python- whether the even

An integer that determines whether an even number

Sample input:

7

Sample output:

7不是偶数

Sample input:

8

Sample output:

8是偶数

Code:

a =int(input())
if a % 2 == 0:
    print("%d是偶数" % a)
else:
    print("%d不是偶数" % a)

7-3 jmu-python- triangle area and perimeter requirements

Input triangle with three sides a, b, c, and outputs the calculated area and perimeter. Assuming that the input sides of a triangle shaping is valid data.

Triangle area formula: wherein

s=(a+b+c)/2
import math  #导入math库
math.sqrt(x)  #调用sqrt函数实现开平方运算,x表示要求值的数据

Input format:
each line of input data, a triangle indicates an edge.

Output formats:

area=面积;perimeter=周长,
Area and perimeter 2 decimal places

Sample input:

3
4
5

Sample output:

area=6.00;perimeter=12.00

Code:

import math
a = eval(input())
b = eval(input())
c = eval(input())
s = (a+b+c)/2
area = math.sqrt(s*(s-a)*(s-b)*(s-c))
perimeter = a+b+c
print("area=%.2f;perimeter=%.2f" % (area, perimeter))

7-4 jmu- piecewise l

The title requirement value calculated piecewise the following (x is any real number from a keyboard input):

If the non-numeric input, the output "Input Error!"

Input format:
Enter a real number x in a row.

Output format:
In press line "y = result" output format, wherein the result with two decimal places.

Sample input:

-2

Output Sample:
In the given here corresponding output. E.g:

y=3.00

Code:

# 运行错误时,主动抛出异常
def demo1(x):
    if x > 1:
        y = 2 * x + 1
    elif x > -2:
        y = 3
    else:
        y = -2 * x - 1
    print("y={:.2f}".format(y))


try:
    x = eval(input())
    demo1(x)
except Exception as result:
    print("Input Error!")

Guess you like

Origin www.cnblogs.com/CloudGuest/p/11503363.html