做了三道题目:
1.判断输入的数是否能被5和3整除。
num_A=input(“please enter the number you want:”)
if int(num_A)%5==0 and int(num_A)%3==0: #判断是否可以被5和3整除
print(num_A + ’ can be divided by 3 and 5’)
else:
print(num_A + ’ can\’t be divided by 3 and 5’)
2.输入三个数,其中输出最大两个数之合
num_A=input(‘please enter a number_A:’)
num_B=input(‘please enter a number_B:’)
num_C=input(‘please enter a number_C:’)
if int(num_A)<=int(num_B) and int(num_A)<=int(num_C):
print(‘the sum of tow larger number is: ‘, int(num_B)+int(num_C))
elif int(num_B)<=int(num_A) and int(num_B)<=int(num_C):
print(‘the sum of tow larger number is: ‘, int(num_A)+int(num_C))
else:
print(‘the sum of tow larger numbers is: ‘, int(num_A)+int(num_B))
3.用牛顿逼近法测算平方根
import cmath
calculat_X=input(‘plz enter the number U want to calculat:’)
guess_X=input(‘plz enter the result U guess:’)
x=int(calculat_X)
y=int(guess_X)
if x<=y:print(‘u have entered the wrong number,plz try again’)
else:
while abs(y-cmath.sqrt(x))>=0.0001:
#用牛顿逐步逼近法
y=(y+(x/y))/2
print('the result is',y)
心得:
1.input ()语句中输入为字符,需要int才能转换成整数计算和判断。
2.print()语句中+str 和 ,int
3.if 语句的形式为:
if 条件一:
结果一
elif 条件二:
结果一
else:
结果N