practice01:
import random
num = random.randint(0,100)
isContinue = "y"
while isContinue == "y" or isContinue == "Y" :
guess = int(input("请你输入0到100的一个数:"))
if guess != num :
if guess > num :
print("你猜的过大")
elif guess < num :
print("你猜的过小")
else :
print("猜测正确")
isContinue = input("是否继续游戏(y/n):")
运行结果:
practice02:
num1 = int(input("请你输入第一个数:"))
num2 = int(input("请你输入第二个数:"))
gcd = 1
if num1 >= num2 :
n_min = num1
else :
n_min = num2
#k = min(num1,num2) 直接用min函数直接比较两个数的大小
for i in range(1,n_min+1) :
if(num1 % i == 0 and num2 % i == 0) :
gcd = i
print("num1和num2的最大公约数是:",gcd)
运行结果:
practice03:
import random
sum = 0
for i in range(1,10001) :
x = random.random() * 2 - 1
y = random.random() * 2 - 1
d = (x**2 + y**2)**0.5
if d <= 1 :
sum += 1
pi = 4 * sum / 10000
print("Π的近似值为:%f" %pi)
practice04:
sum = 0
sum_num = 0
positives = 0
negatives = 0
for i in range(1,10001) :
num = int(input("请你输入一个整数:"))
if num > 0 :
positives += 1
sum_num += 1
sum += num
elif num < 0 :
negatives += 1
sum_num += 1
sum += num
else :
sum_num += 1
average = sum / (i-1)
break
print( "正数个数为:" ,positives)
print("负数个数为:" ,negatives)
print("总个数为:" ,sum_num)
print("平均值为:" ,average)
运行结果:
practice05:
#120 = 2 * 2 * 2 * 3 * 5
#最小因子只有 2,3,5, 7
num = int(input("请你输入一个正整数:"))
print("所有的最小因子为:", end="")
while True :
if(num % 2 == 0) :
min_factor = 2
num /= 2
print("%d, "%min_factor, end="")
#如果2除尽表示,最小因子都为2,结束循环
elif num == 1 :
break
#如果没被2整除,就用3开始除
elif(num % 2 != 0) :
if(num % 3 == 0) :
min_factor = 3
num /= 3
print("%d, "%min_factor, end="")
elif num == 1 :
break
elif(num % 3 != 0) :
if(num % 5 == 0) :
min_factor = 5
num /= 5
print("%d, "%min_factor, end="")
elif num == 1 :
break
elif(num % 5 != 0) :
if(num % 7 == 0) :
min_factor = 7
num /= 7
print("%d, "%min_factor, end="")
elif num == 1 :
break
运行结果:
practice06:
n=int(input("请你输入行数(1-15):"))#行数
for i in range(1,n+1):
space_num = 2 * n - 2 * i #空格数
for j in range(space_num, 0, -1):
print(" ",end="")
k = i # k为每列的最大数
for j in range(k, 0, -1):
print(j,end=" ")
if k==1:
print()
if k>1 and j==1:
for j in range(j,k):
print(j+1,end=" ")
if j==k-1:
print()
运行结果:
practice07:
n = int(input("请你输入行数:"))
for i in range(1,n+1) :
k = i #每行的最大数
for j in range(1, k+1) :
print(j, end=" ")
if j == k :
print("")
print("------------------------")
for i in range(n, 0, -1) :
k = i
for j in range(1, k+1) :
print(j, end=" ")
if j == k :
print("")
print("------------------------")
for i in range(1,n+1) :
space_num = 12 - 2 * i
for j in range(space_num, 0, -1) :
print(" ", end="")
k = i #每行的最大数
for j in range(1, k+1) :
print(j, end=" ")
if j == k :
print("")
print("------------------------")
for i in range(n, 0, -1) :
space_num = 12 - 2 * i
for j in range(0, space_num, 1) :
print(" ", end="")
k = i
for j in range(1, k+1) :
print(j, end=" ")
if j == k :
print("")
运行结果:
practice08:
n = int(input("请你输入行数:"))
for i in range(1,n+1) :
space_num = 2 * n - 2 * i #空格数
for j in range(space_num, 0, -1):
print(" ",end="")
for j in range(1, i + 1) :
num1 = 2 ** (j - 1)
print(num1, end=" ")
for j in range(i-1, 0, -1) :
num2 = 2 ** (j-1)
print(num2, end=" ")
if j == 1 :
print("\n")
运行结果:
practice09:
i_num = 0
for i in range(2,1001):
j = 2
for j in range(2,i) :
if(i % j == 0) :
break
else :
i_num += 1 #质数的个数
print(i , end="\t")
if i_num % 8 == 0 :
print("\n")
practice10:
#(-1)^i+1 / 2i-1
i = int(input("请你输入i的值"))
sum = 0
for j in (1, i+1) :
sum += (-1)**(j+1) / (2*j-1)
pi = 4 * sum
print("Π的近似值为:%f" %pi)
practice11:
#1 / n! i * (i-1) * (i-2) *......* 1
e = 1
item = 1
j = int(input("请输入j的值:"))
for i in range(1, j) :
e += item
item /= i+1
print("e的近似值为:", e)
practice12:
#完全数
print("1000以内的完全数有:" , end="")
for i in range(1,1001) :
sum = 0
for j in (1,i) :
if(i % j == 0) :
sum += i
if sum == i :
print(i, end=" ")
practice13:
#0代表石头、1代表剪刀、2代表布
import random
peopel_win = 0
computer_win = 0
while True :
compuer = random.randint(0,2)
people = int(input("请你随机出(0代表石头、1代表剪刀、2代表布):"))
if people != 0 and people != 1 and people != 2 :
people = int(print("你输入错误,请重新输入(0代表石头、1代表剪刀、2代表布):"))
if (compuer == 0 and people == 1) or (compuer == 1 and people ==2) or (compuer == 2 and people == 0) :
print("电脑获胜")
computer_win += 1
elif (people == 0 and compuer == 1) or (people == 1 and compuer == 2) or (people == 2 and compuer == 0) :
print("你获得胜利!")
peopel_win += 1
else :
print("平局!")
if computer_win == 2 :
print("电脑获得最终胜利!")
break
elif peopel_win == 2 :
print("你最终获得胜利!")
break
practice14:
#输入整数,找出最大值,并且计算出它出现的次数
count = 0
for i in range(1,10000) :
num = int(input("请你输入一个整数:"))
#把第一个值赋值给max
if i == 1 :
max = num
temp = max #寄存第一次的值
#elif i == 2 :
else :
if max == num :
count += 1
elif num > max :
count = 1
max = num
if num == 0 :
#如果第一次输入的值就是最大值,要把这次补加上来
if max == temp :
count += 1
print("最大数为:",max)
print("最大数出现的次数为:",count)
break
pracitce15:
#输入十进制,输出二进制
#利用基本的数学方法,
decimal = int(input("请你输入一个十进制的数:"))
binary = ""
while True :
if decimal % 2 == 1 :
binary += "1"
decimal //= 2
if decimal == 1 :
binary += "1"
break
elif decimal % 2 == 0 :
binary += "0"
decimal //= 2
if decimal == 1 :
binary += "1"
break
#倒序输出
print(binary[::-1] )
practice16:
num = int(input("请你输入一个十进制的数:"))
a = [0,1,2,3,4,5,6,7,8,9,"A","B","C","D","E","F"]
b = []
while True :
s = num // 16
y = num % 16
b = b + [s]
if s // 16 == 0 :
b = b + [y]
break
num = s
for i in b :
print(a[i], end="")
print()
practice17:
import random
odd_number_num = 0 #奇数次数
for i in range(1, 1000001) :
x = random.random() * 2 -1
y = random.random() * 2 -1
k = y / (x-1)
if x < 0 or (k < -1 and x > 0 and y > 0) :
odd_number_num += 1
probability = odd_number_num / 1000000
print("落入奇数区域的概率:", probability)
practice18:
#普通闰年:公历年份是4的倍数且不是100的倍数为普通闰年(如2004、2020年就是闰年)。
#世纪闰年:公历年份是整百数的,必须是400的倍数才是世界闰年(如2000是世纪闰年,1900不是世纪闰年)。
num = 0
for years in range(2001,2100) :
if years % 4 == 0 and years % 100 != 0 :
num += 1
print(years, end = " ")
elif num % 8 == 0 :
print("")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)