分支结构(if-elif-else语句)
百分制成绩转换成等级制
# 规则: E:60分以下;D:60-70;C:70-80; B:80-90; A:90-100
grade = int(input('请输入成绩:'))
if grade < 60:
print('E')
elif grade < 70:
print('D')
elif grade < 80:
print('C')
elif grade < 90:
print('B')
else:
print('A')
随机投掷骰子,根据1-6不同数字做不同指令
from random import randint
x = randint(1,6)
if x == 1:
print('向左移动1')
elif x == 2:
print('向下移动2')
elif x == 3:
print('向下移动3')
elif x == 4:
print('向上移动4')
elif x == 5:
print('向右移动5')
else:
print('向左移动6')
输入三个数,按照从小到大的顺序输出
# 方法一
a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
if a > b :
# 交换a , b的值,语法:元组
a , b = b , a
if b > c :
b , c = c ,b
if a > b :
a , b = b ,a
print(a, b, c)
#方法二
a = float(input('a = '))
b = float(input('b = '))
c = float(input('c = '))
(a, b) = a > b and (b, a) or (a, b)
(b, c) = b > c and (c, b) or (b, c)
(a, b) = a > b and (b, a) or (a, b)
print(a, b, c)
循环结构
-
for循环
for循环在明确知道循环次数时使用
从1加到100的和
my_sum = 0
for num in range(1, 101):
my_sum += num
print(my_sum)
投掷骰子60000,计算分别摇到1-6的总次数
from random import randint
f1 = f2 = f3 = f4 = f5 = f6 = 0
for x in range(60000):
face = randint(1, 6) # 注意:逗号后面需要加空格
if face == 1:
f1 += 1
elif face == 2:
f2 += 1
elif face == 3:
f3 += 1
elif face == 4:
f4 += 1
elif face == 5:
f5 += 1
else:
f6 += 1
print('摇到1的次数是%d次' % f1)
print('摇到2的次数是%d次' % f2)
print('摇到3的次数是%d次' % f3)
print('摇到4的次数是%d次' % f4)
print('摇到5的次数是%d次' % f5)
print('摇到6的次数是%d次' % f6)
-
while循环
while在不知道循环次数时使用,若while后面的条件为真,循环就一直执行,若while后面的条件为假,循环则终止
人猜数字游戏
from random import randint
answer = randint(1, 100)
counter = 0
game_over = False
# while 布尔类型为真时就执行循环,当布尔类型为假,则终止循环
while not game_over:
your_answer = int(input('请输入:'))
counter += 1
if your_answer > answer:
print('小一点!')
elif your_answer < answer:
print('大一点!')
else:
print('恭喜您,猜对啦!')
# 当用户猜对数字时,应该结束循环,布尔类型为假时while循环终止,所以把game_over设定为真,则 not game_ove为假
game_over = True
if counter > 7:
print('智商捉急')
-
分支和循环结构的练习
计算机猜数字
from random import randint
your_answer = int(input('请输入数字:'))
com_answer = randint(0, 100)
print(com_answer)
game_over = False
com_min = 0
com_max = 101
while not game_over:
if com_answer < your_answer:
com_min = com_answer
print('太小啦,请重新输入')
com_answer = randint(com_min+1, com_max-1)
print(com_answer)
elif com_answer > your_answer:
com_max = com_answer
print('太大啦,请重新输入')
com_answer = randint(com_min+1, com_max-1)
print(com_answer)
else:
print('您的数字被猜出来了')
game_over = True
输入一个正整数,判断是否是质数
from math import sqrt
num = int(input('输入正整数'))
is_prime = True
for factor in range(2,int(sqrt(num))+1):
if num % factor ==0:
is_prime = False
break
if is_prime :
print('%d是质数' % num)
else:
print('%d不是质数' % num)
2到100内所有质数打印出来
from math import sqrt
for x in range(2, 101):
is_prime = True
for y in range(2, int(sqrt(x))+1):
if x % y == 0:
is_prime = False
break
if is_prime :
print(x, end='\t')
找10000以内的完美数
for perfect in range(1, 10001):
all_prime = 0
for prime in range (1, int(perfect//2)+1) :
if perfect % prime == 0 :
all_prime += prime
if perfect == all_prime :
print(perfect)
嵌套循环 >> 打印图形
'''
1
12
123
1234
12345
123456
'''
n = int(input('n='))
for row in range(1, n+1):
for col in range(1, row+1):
print(col, sep='', end='')
print()
'''
*
**
***
****
'''
for num in range (1,n+1):
print('*'* num)
'''
A
AB
ABC
ABCD
'''
for row in range (1, n+1):
for col in range (65, 65+row):
print(chr(col), end='')
print()
'''
A
BB
CCC
DDDD
'''
# 方法一
for row in range (1, n +1):
for col in range (65, 65+row):
if col == 64+row:
print(chr(col)*row, end='')
print()
# 方法二
for row in range(n):
for col in range (row+1):
print(chr(65+row), end='')
print()
'''
*
***
*****
*******
'''
y = n
for row in range(1, 2*n, 2):
for _ in range(1):
y -= 1
print(' '* y, '*' * row, end='')
print()
'''
*
**
***
****
'''
y = n
for row in range(1, n+1):
for _ in range(1):
y -= 1
print(' '* y, '*' * row, end='')
print()
捕鱼问题
# 有五个人晚上去捕鱼,第二天早上A把鱼平分成5份,多了1条,被扔掉了,A拿走了自己那一份,
# B把剩下的鱼分成5份,也多了1条,被扔掉了,剩下的人
# CDE也这样做,问至少捕了多少条鱼?
# 方法一
n = 1
while True :
n +=1
mean = n
for _ in range(4):
if (mean - 1) % 5 == 0:
mean = (mean - 1) / 5 * 4
else:
break
if (mean - 1) % 5 == 0:
break
print(n)
# 方法二
fish = 1
while True :
total = fish
is_enough = True
for _ in range(5):
if (total - 1) % 5 == 0:
total = (total - 1) / 5 * 4
else:
is_enough = False
fish += 1
break
if is_enough:
print(fish)
break
# 方法三
fish = 6
is_prime = True
while is_prime:
fish += 5
temp = fish
for x in range(5):
if (temp - 1) % 5 == 0:
temp = (temp - 1) / 5 * 4
if x == 4:
is_prime = False
print(fish)
穷举法:买鸡
# 公鸡5元一只,母鸡3元一只,小鸡1元3只,用100元买100只鸡,公鸡,母鸡,小鸡各多少只?
# 假设公鸡x只,母鸡y只,小鸡z只
for x in range(21):
for y in range(34):
for z in range(0, 100, 3):
if x+y+z == 100 and 5*x+3*y+z//3 == 100 :
print(x,y,z)
print('if总共循环%d次'% (21*34*34))
for x in range(21):
for y in range(34):
z = 100-x-y
if 5*x+3*y+z//3 == 100 and z%3 == 0:
print(x,y,z)
print('if总共循环%d次'% (21*34))
计算机猜数字
from random import randint
your_answer = int(input('请输入数字:'))
com_answer = randint(0, 100)
print(com_answer)
game_over = False
com_min = 0
com_max = 101
while not game_over:
if com_answer < your_answer:
com_min = com_answer
print('太小啦,请重新输入')
com_answer = randint(com_min+1, com_max-1)
print(com_answer)
elif com_answer > your_answer:
com_max = com_answer
print('太大啦,请重新输入')
com_answer = randint(com_min+1, com_max-1)
print(com_answer)
else:
print('您的数字被猜出来了')
game_over = True
赌博游戏
# 规则:玩家掷两个骰子,每个骰子点数为1-6,如果第一次点数和为7或11,则玩家胜;
# 如果点数和为2、3或12,则玩家输庄家胜。若和为其他点数,则记录第一次的点数和,
# 玩家继续掷骰子,直至点数和等于第一次掷出的点数和,则玩家胜;若掷出的点数和为7则庄家胜。
from random import randint
first1 = randint(1, 6)
first2 = randint(1, 6)
first_sum = first1 + first2
print(first_sum)
if first_sum == 7 or first_sum == 11 :
print('玩家胜利!')
elif first_sum == 2 or first_sum == 3 or first_sum == 12 :
print('庄家胜利!')
else:
while True :
current1 = randint(1, 6)
current2 = randint(1, 6)
current_sum = current1 + current2
print(current_sum)
if current_sum == first_sum :
print('玩家胜利!')
break
if current_sum == 7 :
print('庄家胜利!')
break
# 在上面的游戏加入赌注,规则:玩家每一局可单独下赌注,赢则加赌注,输则减赌注
# 玩家每一局结束后可选择继续游戏或结束游戏
from random import randint
fund = int(input('你的赌资:'))
is_continue = True
while is_continue :
wager = int(input('请下注:'))
first1 = randint(1, 6)
first2 = randint(1, 6)
first_sum = first1 + first2
print(first_sum)
if first_sum == 7 or first_sum == 11 :
fund += wager
print('玩家胜利!你目前的赌资还有:¥%d元' % fund)
your_choice = int(input('是否继续游戏(继续输入1,结束输入2):'))
if your_choice == 1 :
continue
if your_choice == 2 :
is_continue = False
elif first_sum == 2 or first_sum == 3 or first_sum == 12 :
fund -= wager
print('庄家胜利!你目前的赌资还有:¥%d元' % fund)
your_choice = int(input('是否继续游戏(继续输入1,结束输入2):'))
if your_choice == 1 :
continue
if your_choice == 2:
is_continue = False
else:
while True :
current1 = randint(1, 6)
current2 = randint(1, 6)
current_sum = current1 + current2
print(current_sum)
if current_sum == first_sum :
fund += wager
print('玩家胜利!你目前的赌资还有:¥%d元' % fund)
your_choice =int(input('是否继续游戏(继续输入1,结束输入2):'))
if your_choice == 1:
break
if your_choice == 2 :
is_continue = False
break
if current_sum == 7 :
fund -= wager
print('庄家胜利!你目前的赌资还有:¥%d元' % fund)
your_choice = int(input('是否继续游戏(继续输入1,结束输入2):'))
if your_choice == 1:
break
if your_choice == 2 :
is_continue = False
break
print('游戏结束!')