Python3 循环和判断小练习
设计一个函数, 在桌面上创建10个文本, 以数字给它们命名
def text_creation():
path = r'C:\Users\Black\Desktop\test\\'
for name in range(1, 11):
with open(path + str(name) + '.txt', 'w', encoding='utf-8') as f:
f.write(str(name))
print('Done!')
text_creation()
设计一个复利计算函数 invest(), 它包含三个参数: amount(资金), rate(利率), time(投资时间). 输入每个参数后调用函数, 应该返回每一年的资金总额
def invest(amount, rate, time):
for year in range(1, time + 1):
amount = amount * (1 + rate)
print(f'year {year} : ${amount}')
invest(100, 0.05, 10)
'''
year 1 : $105.0
year 2 : $110.25
year 3 : $115.7625
year 4 : $121.55062500000001
year 5 : $127.62815625000002
year 6 : $134.00956406250003
year 7 : $140.71004226562505
year 8 : $147.74554437890632
year 9 : $155.13282159785163
year 10 : $162.8894626777442
'''
摇骰子(3个), 猜大小. 点数小于10则为小, 大于10则为大
import random
def dice_game():
while True:
print('<<<< GAME STARTS! >>>>>')
point1 = random.randrange(1, 7)
point2 = random.randrange(1, 7)
point3 = random.randrange(1, 7)
lis = [point1, point2, point3]
if sum(lis) <= 10:
result = 'Small'
else:
result = 'Big'
guess = input('Big or Small: ')
if guess in ['Big', 'Small']:
print('<<<< ROLL THE DICE! >>>>>')
if guess == result:
print(f'The point are {lis} You Win!')
else:
print(f'The point are {lis} You Lose!')
break
else:
print('Invalid Words!')
dice_game()
在上一个项目的基础上增加下注功能, 赔率默认为1, 初始金额为1000, 当金额为0时退出游戏
import random
def dice_game():
money = 1000
while True:
print('<<<< GAME STARTS! >>>>>')
point1 = random.randrange(1, 7)
point2 = random.randrange(1, 7)
point3 = random.randrange(1, 7)
lis = [point1, point2, point3]
if sum(lis) <= 10:
result = 'Small'
else:
result = 'Big'
guess = input('Big or Small: ')
bet = int(input('How much you wanna bet? - '))
if money - bet < 0:
print('余额不足!')
continue
if guess in ['Big', 'Small']:
print('<<<< ROLL THE DICE! >>>>>')
if guess == result:
print(f'The point are {lis} You Win!')
money += bet
print(f'You gained {bet}, you have {money} now!')
else:
print(f'The point are {lis} You Lose!')
money -= bet
print(f'You lose {bet}, you have {money} now!')
else:
print('Invalid Words!')
if money == 0:
print('GAME OVER')
break
dice_game()
给定各运营商号段, 判断用户输入号码的运营商, 要求如下:
- 号码长度不少于11位
- 输入的号码必须是数字
- 号码是运营商号段中的一个号码
def number_verification():
CN_mobile = [134, 135, 136, 137, 138, 139, 150, 151, 152, 157, 158, 159, 182, 183, 184, 187, 188, 147, 178, 1705]
CN_union = [130, 131, 132, 155, 156, 185, 186, 145, 176, 1709]
CN_telecom = [133, 153, 180, 181, 189, 177, 1700]
while True:
number = input('Enter your number: ')
if not number.isdigit():
print('Invalid input, please enter digits')
continue
if not len(number) == 11:
print('Invalid length, your number should be in 11 digits')
continue
first_three = int(number[0:3])
first_four = int(number[0:4])
if first_four in CN_mobile or first_three in CN_mobile:
print('Operator: China mobile')
print(f'We are sending verification code via text to your phone: {number}')
break
elif first_four in CN_union or first_three in CN_union:
print('Operator: China union')
print(f'We are sending verification code via text to your phone: {number}')
break
elif first_four in CN_telecom or first_three in CN_telecom:
print('Operator: China telecom')
print(f'We are sending verification code via text to your phone: {number}')
break
else:
print('No such a operator!')
number_verification()
Python3 循环和判断小练习的更多相关文章
- Java 变量、循环、判断
粗糙笔记不喜勿喷 Java 8大基本类型 第一类:逻辑型(boolean) 1.boolean类型只存在true(真),false(假)两种形式 例: boolean a=true; boolean ...
- Python3 循环语句
Python3 循环语句 转来的 很适合小白 感谢作者 Python中的循环语句有 for 和 while. Python循环语句的控制结构图如下所示: while 循环 Python中wh ...
- 【python】Python3 循环语句
[python]几种常见的循环 注意:如果涉及到程序中print语句中含有%d,%s,那么要在脚本最开始写语句:#coding=utf-8,才能够正常输出想要的数字或者字符串. Python3 循环语 ...
- smarty基本用法,循环,判断
require './smarty/Smarty.class.php'; $sm = new Smarty; $sm->setTemplateDir("./dir");//设 ...
- Python3循环语句
Python3 循环语句 Python中的循环语句有for和while. 循环语句控制结构图如下: 一.while循环 ①循环结构 while 判断条件: 执行语句 实例: n = int(input ...
- python013 Python3 循环语句
Python3 循环语句本章节将为大家介绍Python循环语句的使用.Python中的循环语句有 for 和 while.Python循环语句的控制结构图如下所示: while 循环Python中wh ...
- .NET Core CSharp初级篇 1-2 循环与判断
.NET Core CSharp初级篇 1-2 本节内容循环与判断 循环 循环是一个在任何语言都是极为重要的语法,它可以用于很多东西,例如迭代数组等等.在C#中,语法层面的循环有:for , fore ...
- python基本数据类型和循环、判断
一.语言分为2种: 编译型语言:写完代码不能执行,得先编译 c.c++.c#,速度相对解释性语言更快,因为只需要执行一次解释型语言:不需要编译,直接执行 python.java.php.js.go.r ...
- mysql存储过程查询结果循环遍历 判断 赋值 游标等基本操作
一.首先说下本篇博客所实现功能的背景和功能是怎样的: 背景:因为公司项目开始迁移新平台项目,所以以前的平台老数据以及订单信息需要拆分表,而且需要业务逻辑来分析以前的订单表,来拆分成另外的几个新表,包括 ...
随机推荐
- hdu 1028 Sample Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 从0开始学前端(笔记备份)----HTML部分 Day2 HTML表格表单
- markdown总结 (webstrom快捷键)
# 在HbuilderX中写markdown(WebStrom快捷键配置)0. 一些快捷键和鼠标操作:1. ctrl+shift+↑ 当前行或者选中的块整体向上移动 ↓同理2. 向两侧扩大选择:A ...
- Python 并发总结,多线程,多进程,异步IO
1 测量函数运行时间 import time def profile(func): def wrapper(*args, **kwargs): import time start = time.tim ...
- Django2.0--创建缓存表
创建缓存表 在项目的虚拟环境下(若有),执行:python manage.py createcachetab
- 【漏洞复现】Apache Solr远程代码执行(CVE-2019-0193)
0x01 概述 Solr简介 Apache Solr 是一个开源的企业级搜索服务器.Solr 使用 Java 语言开发,主要基于 HTTP 和 Apache Lucene 实现.Apache Solr ...
- 前端vue实现pdf文件的在线预览
3.前端vue实现pdf文件的在线预览 我是通过 <iframe> 标签就可以满足我工作的 pdf预览需求 如果<iframe> 无法满足需求 , 可以使用pdf.js这个插件 ...
- Java package 包的命名规范。
Java的包名都有小写单词组成,类名首字母大写:包的路径符合所开发的 系统模块的 定义,比如生产对生产,物资对物资,基础类对基础类.以便看了包名就明白是哪个模块,从而直接到对应包里找相应的实现. 由于 ...
- sql 删除表数据并使ID自增重置
方法1:truncate table 你的表名//这样不但将数据全部删除,而且重新定位自增的字段 方法2:delete from 你的表名dbcc checkident(你的表名,reseed,0) ...
- jsp html 实现隐藏输入框,点击可以取消隐藏&&弹出输入框
jsp代码: <script language="javascript" type="text/javascript"> function chg ...