import random
computer_number = random.randint(0,100)
print("\t\t欢迎来到猜数字游戏!")
person_number = int(input("请输入0-100的数字:"))
while computer_number != person_number :
if person_number < computer_number :
print("您猜的数字小了!")
person_number = int(input("请重新猜测:"))
if person_number > computer_number :
print("您猜测的数字大了!")
person_number = int(input("请重新猜测:"))
print("您终于猜对了!")
demo 02
n1,n2 = eval(input("请输入两个数"))
if n1 > n2 :
little = n2
elif n1 < n2 :
little = n1
else :
print("最大公约数为:"+str(n1))
exit(0)
for i in range(1,little + 1) :
if n1 % i == 0 and n2 % i == 0:
GCD = i
print("最大公约数为:"+str(GCD))
demo 03
import random
import math
n = 1
numberOfHits = 0
while n <= 1000000 :
x = random.uniform(0,1)
y = random.uniform(0,1)
length = math.sqrt(x**2 + y**2)
if length <= 1 :
numberOfHits += 1
n += 1
PI = 4 * numberOfHits / 1000000
print("π的值为:",PI)
demo 04
demo 05
sum = 0
i = 0
j = 0
n = 1
time = -1
while n != 0 :
time += 1
n = int(input("请输入数据:"))
sum += n
if n > 0 :
i += 1
elif n < 0 :
j += 1
average = sum / time
print("正数的个数为:%s\n负数的个数为:%s"%(i,j))
print("总共有%s个数\n平均数为:%s"%(time,average))
demo 06
n = int(input("请输入一个整数:"))
i = 2
while True:
if n%i == 0:
print(i,end=",")
n /= i
else:
i += 1
if n == 1:
break
demo 07
height = int(input("需要的打印金字塔的高度:"))
for i in range(0,height) :
empty = height - i - 1
for j in range(0,empty) :
print(' ',end = "")
number = 2 * (i + 1) - 1
left = i + 1
for k in range(left,0,-1) :
print(k,end = " ")
for k in range(2,left + 1) :
print(k,end = " ")
print('')
demo 08
line = int(input("请输入几行:"))
for i in range(0 , line) :
for j in range(0 , i + 1) :
print(j + 1,end = " ")
print('')
print('')
for i in range(0 , line) :
for j in range(0 , line - i) :
print(j + 1,end = " ")
print('')
print('')
for i in range(0 , line) :
kong = line - i - 1
for j in range(0 , kong) :
print(' ',end = "")
for j in range(i + 1 , 0 , -1) :
print(j,end = " ")
print('')
print('')
for i in range(0 , line) :
for j in range(0 , i) :
print(' ',end = "")
for j in range(0 , line - i) :
print(j + 1,end = " ")
print('')
demo 09
line = int(input("请输入几行"))
for i in range(0 , line) :
kong = line - i - 1
for j in range(0 , kong) :
print(' ',end = "")
left_number = i + 1
for j in range(1 , left_number + 1) :
if 2 ** (j - 1) < 10 : #if控制一位和两位三位数字之间的间隔
print(2 ** (j - 1),end = " ")
elif 10 <= 2 ** (j - 1) < 100:
print(2 ** (j - 1),end = " ")
else :
print(2 ** (j - 1),end = " ")
for j in range(0 , i) :
x = (2 ** i) * (0.5 ** (j + 1)) #金字塔右边数字
x = int(x)
if x < 10 :
print(x,end = " ")
elif 10 <= x < 100 :
print(x,end = " ")
else :
print(x,end = " ")
print('')
demo 10
n = 0
time = 0
for i in range(2 , 1001) :
for j in range(1 , i + 1) :
if i % j == 0 :
n += 1
if n == 2 :
print(i,end = " ")
time += 1
if time % 8 == 0 :
print('')
else :
n = 0
demo 11
s = 0
i = int(input("请输入i的值:"))
for j in range(0 , i) :
s += 1 / (1 + 2 * j)
s *= -1
print("π的近似值:"+str(4 * s))
demo 12
s = 1
e = 0
time = 1
i = int(input("请输入次数:"))
while time <= i :
for j in range(time) :
s = s * (j + 1)
time += 1
e = 1 / s
s = 1
print("e的近似值是:",e)
demo 13
s = 0
for i in range(2 , 1000) :
for j in range(1 , i) :
if i % j == 0 :
s += j
if s == i :
print("1000以内的完全数有:",i)
s = 0
demo 14
import random
play1 = 0
play2 = 0
a = ['剪刀','石头','布']
while play1 < 2 and play2 < 2 :
computer = random.choice(a)
person = input("请出剪刀石头布:")
if person == computer :
print("平局!")
elif (person == '剪刀' and computer == '布') or (person == '布' and computer == '石头') or (person == '石头' and computer == '剪刀') :
play1 += 1
print("你赢!")
else :
play2 += 1
print("电脑赢!")
if play1 == 2 :
print("你胜利了!")
else :
print("呜呜呜~~你输了!")
demo 15
number = 1
time = 0
max_number = int(input("请输入数字以0结束:"))
while number != 0 :
number = int(input("请输入数字以0结束:"))
if number > max_number :
time = 0
if number > max_number :
max_number = number
if number == max_number :
time += 1
print("最大数为:",max_number)
print("一共出现的次数为:",time)
demo 16
a = ''
time = 0
number = int(input("请输入一个十进制的数:"))
while number != 1 :
n = number % 2
a += str(n)
time += 1
number = number // 2
for i in range(time , 0 , -1) :
print(a[i - 1],end = " ")
demo 17
A=int(input('请输入10进制数'))
print("十六进制为{:X}".format(A))
demo 18
import random
import math
n = 0
time = 1
while time <= 1000000 :
x = random.uniform(-10,10)
y = random.uniform(-10,10)
if x < 0 :
n += 1
elif x > 0 and y > 0 and x + y < 10 :
n += 1
time += 1
print("落在奇数区域的概率为:",n / 1000000)
demo 19
line = 0
for i in range(2001 , 2101) :
if (i % 4 == 0 and i % 100 != 0) or i % 400 == 0 :
print(i,end = " ")
line += 1
if line % 10 == 0 :
print('')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)