分类:Python 常用100例

20 篇文章

20-while循环,累加至100
因为循环次数是已知的,实际使用时,建议用for循环 sum100 = 0 counter = 1 while counter < 101: sum100 += counter counter += 1 print(sum100)
19-猜数,5次机会
import random num = random.randint(1, 10) counter = 0 while counter < 5: answer = int(input('guess the number: ')) if answer > num: print('猜大了') elif answer < num: pr…
18-猜数,直到猜对
import random num = random.randint(1, 10) running = True while running: answer = int(input('guess the number: ')) if answer > num: print('猜大了') elif answer < num: print(…
17-改进的石头剪刀布
import random all_choices = ['石头', '剪刀', '布'] win_list = [['石头', '剪刀'], ['剪刀', '布'], ['布', '石头']] prompt = """(0) 石头 (1) 剪刀 (2) 布 请选择(0/1/2): """ computer = random.choice(all_…
16-石头剪刀布
import random all_choices = ['石头', '剪刀', '布'] computer = random.choice(all_choices) player = input('请出拳: ') # print('Your choice:', player, "Computer's choice:", computer) pri…
15-成绩分类2
score = int(input('分数: ')) if score >= 60 and score < 70: print('及格') elif 70 <= score < 80: print('良') elif 80 <= score < 90: print('好') elif score >= 90…
14-成绩分类1
score = int(input('分数: ')) if score >= 90: print('优秀') elif score >= 80: print('好') elif score >= 70: print('良') elif score >= 60: print('及格') else: print('你要努力了')
13-猜数:基础实现
import random num = random.randint(1, 10) # 随机生成1-10之间的数字 answer = int(input('guess a number: ')) # 将用户输入的字符转成整数 if answer > num: print('猜大了') elif answer < num: print('…