-
代码1:
d = 0
def math(a,b,c):
d = b ** 2 -4 * a * c
if d == 0:
r = -b/2a
print(‘Enter roots is %.2f’%®)
elif d > 0:
r1 = (-b + d**(1/2))/2a
r2 = (-b - d**(1/2))/2*a
print(‘Enter roots are %f and %f’%(r1,r2))
else:
print(‘The equation has no real roots’)
def start():
a,b,c =map(float,input(‘请输入a,b,c:’).split(’,’))
d = 0
math(a,b,c)
start()
结果:
请输入a,b,c:1,3,1
Enter roots are -0.381966 and -2.618034
- 代码2:
import random
num1 = random.randint(1,100)
num2 = random.randint(1,100)
def main():
print(‘计算%d和%d的和’%(num1,num2))
C = C_sum(num1,num2)
U = U_sum()
if C == U:
print(‘Ture’)
else:
print(‘False’)
def C_sum(num1,num2):
sum = num1 + num2
return sum
def U_sum():
sum = int(input(‘两数之和为:’))
return sum
main()
运行结果:
计算64和1的和
两数之和为:65
Ture
- 代码3:
def main():
N = int(input(‘Enter today is day(周日为0,周一为1…周六为6):’))
F = int(input(‘Enter the number of days elapsed since today:’))
Nday = mathtoday(N)
Fday = mathFday(N,F)
print(‘Today is %s and the future day is %s’%(Nday,Fday))
def mathtoday(Fday):
if Fday == 0:
today = ‘Sunday’
elif Fday == 1:
today = ‘Monday’
elif Fday == 2:
today = ‘Tuesday’
elif Fday == 3:
today = ‘Wednesday’
elif Fday == 4:
today = ‘Thursday’
elif Fday == 5:
today = ‘Friday’
elif Fday == 6:
today = ‘Saterday’
return today
def mathFday(Nday,Fday):
Fday1 = (Nday + Fday) % 7
return mathtoday(Fday1)
main()
运行结果:
Enter today is day(周日为0,周一为1…周六为6):3
Enter the number of days elapsed since today:0
Today is Wednesday and the future day is Wednesday
- 代码4:
def main():
num1,num2,num3 = map(int,input('请输入仨个整数:').split(','))
list1 = [num1,num2,num3]
math(list1)
print(list1)
def math(list1):
# list2 = (sorted(list1))
return list1.sort()
# return list2
main()
运行结果:
请输入仨个整数:2,5,3
[2, 3, 5]
- 代码5:
def main():
weight1,price1 = map(float,input(‘Enter weight and price for package1:’).split(’,’))
weight2,price2 = map(float,input(‘Enter weight and price for package2:’).split(’,’))
package1 = math(weight1,price1)
package2 = math(weight2,price2)
math1(package1,package2)
def math1(package1,package2):
if package1 > package2:
print(‘package2 has the better price.’)
elif package1 < package2:
print(‘package1 has the better price.’)
else:
print(‘price alike.’)
def math(weight,price):
unitPrice = price/weight
return unitPrice
main()
运行结果:
计算64和1的和
两数之和为:65
Ture
- 代码6:
def main():
year,month = map(int,input(‘输入年月:’).split(’,’))
math(year,month)
def math(year,month):
list1 = [1,3,5,7,8,10,12]
#优先级关系:or<and<not,同一优先级默认从左往右计算。
if year % 400 == 0 or year % 100 != 0 and year % 4 == 0 :
if month ==2:
print(‘这个月有29天’)
elif month in list1:
print(‘这个月有31天’)
else:
print(‘这个月有30天’)
else:
if month ==2:
print(‘这个月有28天’)
elif month in list1:
print(‘这个月有31天’)
else:
print(‘这个月有30天’)
main()
结运行果:
输入年月:2000,5
这个月有31天
- 代码7:
import random
def main():
U_s = random.randint(1,2)
print(U_s)
C_s = input(‘猜硬币是正面还是反面:’)
if C_s == ‘正面’:
C_s = 1
else:
C_s = 2
math(C_s,U_s)
def math(C_s,U_s):
if U_s == C_s:
print('你猜对了')
else:
print('你猜错了')
main()
运行结果:
猜硬币是正面还是反面:正面
你猜对了
- 代码8:
import random
def main():
C_s = random.randint(0,2)
U_s = int(input(‘scissor (0),rock(1),paper(2):’))
math(C_s,U_s)
def math(C_s,U_s):
if C_s == U_s:
print(‘Deuce!’)
elif C_s == 2 and U_s == 0 or C_s == 1 and U_s ==2 or C_s == 0 and U_s ==1 :
print(‘You lose!’)
else:
print(‘You win!’)
main()
代码结果:
scissor (0),rock(1),paper(2):2
You lose!
-
代码9:
-
def main():
year,m,q= map(float,input(‘输入年份,月份,第几天:’).split(’,’))
j = year/1000
k = year%1000
math(q,m,k,j)
def math(q,m,k,j):
h = int((q + (26 * (m + 1)) / 10 + k + k/4 + j/4 + 5 * j) % 7) # m k j
day = ‘’
if h == 0:
day = ‘周六’
elif h == 1:
day = ‘周日’
elif h == 2:
day = ‘周一’
elif h == 3:
day = ‘周二’
elif h == 4:
day = ‘周三’
elif h == 5:
day = ‘周四’
elif h == 6:
day = ‘周五’
else :
print(‘输入有误’)
print(‘Day of the week is %s’%day)
main()
运行结果:
输入年份,月份,第几天:2019,8,25
Day of the week is 周四
1
- 代码10:
import random
def main():
U_s = input(‘是否抽取牌(y/n):’)
if U_s == ‘y’:
math()
else:
print(‘结束’)
def math():
r = random.randint(1,52)
print(‘你抽取的是第%d张牌’%r)
num = random.choice([‘A’,1,2,3,4,5,6,7,8,9,‘J’,‘Q’,‘K’])
variedad = random.choice([‘梅花♣’,‘红桃♥’,‘方块♦’,‘黑桃♠’])
print(‘你抽取的牌为%s的%s’%(variedad,num))
main()
运行结果:
是否抽取牌(y/n):y
你抽取的是第44张牌
你抽取的牌为红桃♥的4
- 代码11:
def main():
num = input('请输入一个三位数:')
num1 =list(num) # str转list
math(num,num1)
def math(num,num1):
for i in range(len(num)):
num1[2-i] = num[i]
num1 =''.join(num1) # list转str
if num == num1:
print('%s是一个回文数'%num)
else:
print('%s不是一个回文数'%num)
main()
运行结果:
请输入一个三位数:121
121是一个回文数
- 代码12:
def main():
a =list(input(‘输入三角形的三边长(用,隔开):’).split(’,’))
math(a)
def math(a):
b=int(’’.join(a[0]))
c=int(’’.join(a[1]))
d=int(’’.join(a[2]))
if b + c > d and b + d > c and d + c > b:
perimeter = b+c+d
print(‘周长为:%d’%perimeter)
else:
print(‘输入不合法!’)
main()
运行结果:
输入三角形的三边长(用,隔开):3,4,5
周长为:12