Demo37
**
**
Guess a magic number between 0 and 100
Enter your guess: 50
Your guess is too high
Enter your guess: 25
Your guess is too 1ow
Enter your guess: 42
Your guess is too high .
Enter your guess: 39
Yes,the number is 39
程序编辑:
"""
数据:电脑随机产生一个数字 用户输入数据
步骤:
0.随机产生一个数字
1.提示用户输入一个数字
2.对该数字和随机数字进行比较
2.1 如果相等 则猜中 程序结束
2.2 如果大于 提示大了 继续执行1
2.3 如果小于 提示小了 继续执行1
"""
import random
com = random.randint(0, 100) # [0,100] range(0,100) # [0,100)
while True:
# 1
num = int(input("Enter a number:"))
# 2.1
if num == com:
print("Yes ,the number is", com)
break
# 2.2
elif num > com:
print("Your guess is too high")
# 2.3
else:
print("Your guess is too low")
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/37.py
Enter a number:33
Your guess is too low
Enter a number:77
Your guess is too high
Enter a number:55
Your guess is too low
Enter a number:66
Your guess is too low
Enter a number:70
Your guess is too low
Enter a number:75
Your guess is too high
Enter a number:73
Your guess is too high
Enter a number:72
Yes ,the number is 72
Process finished with exit code 0
Demo38
程序编辑:
"""
数据:两个数字num1 num2 gcd 所有可能的除数min(num1,num2)
步骤:
1.提示输入两个数字
2.取两者的最小值min 开始遍历 2~min
3.在遍历的过程中 如果 除数 能够把num1 num2都整除 gcd 更新为此数字
4.直到所有的可能都遍历结束 gcd最终的最大公约数
"""
while True:
num1, num2 = eval(input("Enter two numbers:"))
k = min(num1, num2)
gcd = 1
for i in range(2, k + 1):
if num1 % i == 0 and num2 % i == 0:
gcd = i
print(gcd)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/38.py
Enter two numbers:8,24
8
Enter two numbers:989,33
1
Enter two numbers:22,88
22
Enter two numbers:7,21
7
Enter two numbers:
Demo39
蒙特卡罗模拟使用随机数和概率来解决问题。它在计算机数学、物理、化学和经济方面都有非常广泛的应用。现在,我们看一个使用蒙特卡罗模拟来估计π的例子。
首先,绘制一个带外接正方形的圆。假设这个圆的半径为1。因此,这个圆的面积就是π,而矩形的面积为4。在这个正方形内随机产生一个点。这个点落在圆内的概为circleArea/squareArea= π/4。编写一个程序,在正方形内随机产生1000000个点,使用.
numberOfHits表示落人圆内点的个数。所以,numberOfHits 大约就是1 000 000* (π/4)。π
就可以被近似表示为4*numberOfHits/1 000 000。
程序编辑:
"""
数据:总点数total 落入圆内的点数num 若干个随机的坐标
步骤:
1.开启循环 1~1000000
2.每次循环 随机生成一个坐标 x∈[-1,1] y∈[-1,1]
2.1 如果坐标在圆内 num += 1
3.循环结束后 π = num / total * 4
"""
import random
num = 0
total = 1000000
for i in range(0, total):
# [0,1) * 2 => [0,2) - 1 =>[-1,1)
x = random.random() * 2 - 1
y = random.random() * 2 - 1
distance = (x ** 2 + y ** 2) ** 0.5
if distance <= 1:
num += 1
pi = num / total * 4
print(pi)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/39.py
3.141308
Process finished with exit code 0
Demo40
**
**
Enter an integer, the input ends if it is 0: 1
Enter an integer, the input ends if it is 0: 2
Enter an integer, the input ends if it is 0: -1
Enter an integer, the input ends if it is 0: 3
Enter an integer, the input ends if it is 0: 0
The number of positives is 3
The number of negatives is 1
The total is 5
The average is 1.25
Enter an integer, the input ends if it is 0: 0
You didn’t enter any number
程序编辑:
"""
数据:正数的个数 负数的个数 总和 平均值
positives negatives sum average
步骤:
1.提示用户输入一个数字
2.对该数字进行判断
2.1 如果该数字为0 则循环结束
2.2 如果该数字 > 0 positives += 1 sum += 数字 继续1
2.3 如果该数字 < 0 negatives += 1 sum += 数字 继续1
3.计算平均值 = 总和 / (正 + 负)
如果 总个数 为 0 则输出没有任何数字输入
"""
positives = 0
negatives = 0
sum = 0
while True:
num = int(input("Enter a number:"))
if num == 0:
break;
elif num > 0:
positives += 1
else :
negatives += 1
sum += num
total = positives + negatives
if total == 0:
print("You didn't enter any number")
else :
print("The number of positives",positives)
print("The number of negatives",negatives)
print("The sum is",sum)
print("The average is",sum / total)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/40.py
Enter a number:3
Enter a number:2
Enter a number:99
Enter a number:33
Enter a number:-2
Enter a number:3
Enter a number:-88
Enter a number:0
The number of positives 5
The number of negatives 2
The sum is 50
The average is 7.142857142857143
Process finished with exit code 0
Demo41
**
**
2,2,2,3,5
程序编辑:
number = int(input("Enter a number:"))
while number != 1:
flag = True # 循环是否正常结束
for i in range(2, number // 2 + 1):
if number % i == 0:
print(i, end=", ")
number = number // i
flag = False
break
if flag :
print(number,end=", ")
break
"""
else : # 针对for循环的 不是for中的可能
print(number)
break
"""
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/41.py
Enter a number:120
2, 2, 2, 3, 5,
Process finished with exit code 0
Demo42
**
**
Enter the number of 1ines: 7
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
程序编辑:
while True :
number = int(input("Enter a number:"))
for i in range(0, number):
for k in range(number - i, 0, -1):
print(" ",end=" ")
for j in range(-i, i + 1):
print(abs(j) + 1, end=" ")
print()
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/42.py
Enter a number:4
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
Enter a number:7
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
Enter a number:15
1
2 1 2
3 2 1 2 3
4 3 2 1 2 3 4
5 4 3 2 1 2 3 4 5
6 5 4 3 2 1 2 3 4 5 6
7 6 5 4 3 2 1 2 3 4 5 6 7
8 7 6 5 4 3 2 1 2 3 4 5 6 7 8
9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9
10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10
11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11
12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12
13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13
14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Enter a number: