程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] learning notes week7-1 branch

編輯:Python

【PYTHON】 Piecewise function branch

Title Description

This topic requires to calculate the value of the following piecewise function (x Is an arbitrary real number entered from the keyboard ):

If you enter a non number , The output “Input Error!”

Input

Enter a real number in one line x.

Output

Press in a line ”y=result” Format output , among result Keep two decimal places .

The sample input

-2

Sample output

y=3.00

x=input()
if not(x.isalpha()):
x=eval(x)
if x<=-2:
print("y={:.2f}".format(-2*x-1))
elif -2<x<=1:
print("y={:.2f}".format(3))
elif x>1:
print("y={:.2f}".format(2*x+1))
else:
print("Input Error!")

【PYTHON】a Divide b# Branch

Title Description

Calculation a Divide b, The result is rounded to the nearest , Retain 2 Decimal place .

Input

The input consists of two lines , One real number per line

Output

The normal calculation result is a real number , When user input b by 0 Time output " Divide by zero error "

The sample input

5 0

Sample output

Divide by zero error

a=eval(input())
b=eval(input())
if b==0:
print(" Divide by zero error ")
else:
print("{:.2f}".format(a/b))

【PYTHON】kiki Count # Branch

Title Description

problem :KiKi This year, 5 Year old , Have been able to recognize 100 Non negative integers within , And be able to do 100 The addition of nonnegative integers within . however ,BoBo The teacher found that KiKi Greater than or equal to 100 When calculating positive integers of , The following rules :‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ 1. Only the last two digits of the number are retained , for example : Yes KiKi Come on 1234 Equivalent to 34;‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ 2. If the result is greater than or equal to 100, that KIKI Only the last two digits of the calculated results are retained , If ten of the two are 0, Only one bit is reserved .‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ for example :45+80 = 25‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ A nonnegative integer is required a and b, simulation KiKi The operation rules of the algorithm calculate a+b Value .

Input

Two 100 Non negative integers within

Output

kiki The result of the calculation is

The sample input

45 80

Sample output

25

a=eval(input())
b=eval(input())
if a+b>=100:
print("{}".format(a+b-100))
else:
print("{}".format(a+b))

【PYTHON】 Increase subsidies # Branch

Title Description

The community in order to subsidize poor families , For all incomes below 2000 element ( Not included 2000 element ) The family of , The increase is equivalent to its income 30% Subsidies for .‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ Write a program to realize : Enter a list of incomes for several families ( On the same line , Separated by commas ), Calculate and output the list of household income after the increase of subsidies .‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬ Each revenue output is followed by a space , There is no space after the last income .

Input

1500,1800,2100,2500,1200

Output

1950.0 2340.0 2100.0 2500.0 1560.0

The sample input

2000,2023,1980,1000

Sample output

2000.0 2023.0 2574.0 1300.0

a=list(eval(input()))
for i in range(0,len(a)):
if a[i]<2000:
a[i]=a[i]+a[i]*0.3
for i in a:
print("{:.1f}".format(i),end=" ")

【PYTHON】 Body mass index # Branch

Title Description

Body mass index ( English is Body Mass Index, abbreviation BMI), Its value is weight divided by the square of height . The unit of weight is kilogram , height The unit is rice. .BMI At present, it is a standard commonly used in the world to measure the degree of weight and health . Here is 16 People over the age of BMI chart surface

BMI

explain

BMI<18

Super light

18<=BMI<25

standard

25<=BMI<27

overweight

27<=BMI

obesity

Write a program , Enter the user's weight (Kg) And height ( rice ), Show it BMI value , And make an explanatory evaluation .

Input

weight , height

Output

Super light / standard / overweight / One of the obesity .

The sample input

70,1.75

Sample output

standard

a,b=map(eval,input().split(","))
c=a/(b**2)
if c<18:
print(" Super light ")
elif 18<=c<25:
print(" standard ")
elif 25<=c<27:
print(" overweight ")
else:
print(" obesity ")

【PYTHON】 Temperature conversion # character string # Branch

Title Description

There are two different systems of temperature characterization : Centigrade (Celsius) And Fahrenheit (Fabrenheit).‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

Please write a program to convert user input Fahrenheit to Celsius , Or convert the input Celsius to Fahrenheit .‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

The conversion algorithm is as follows :(C In degrees Celsius 、F Is Fahrenheit )‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

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

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

Requirements are as follows :‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

(1) The input and output centigrade are in capital letters C start , The temperature can be an integer or a decimal , Such as :C12.34 It means centigrade 12.34 degree ;‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

(2) The input and output Fahrenheit are in capital letters F start , The temperature can be an integer or a decimal , Such as :F87.65 It means centigrade 87.65 degree ;‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

(3) Do not consider the problem of abnormal input , Output two decimal places ;‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬

(4) Use input() When obtaining test case input , Do not add the prompt string .

Input

C12.34

Output

F54.21

The sample input

C12.34

Sample output

F54.21

a=input()
if a[0]=="C":
temp=eval(a[1:])
print("F{:.2f}".format(temp*1.8+32))
else:
temp=eval(a[1:])
print("C{:.2f}".format((temp-32)/1.8))

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved