一、设计这样一个函数,在指定的文件夹上创建10个文本,以数字给它们命名。

def text_creation():
path ='D:/study/python3/w/'
for name in range (1,11):
with open(path + str(name) + '.txt','w' ) as text:
text.write(str(name))
text.close()
print('Done')
text_creation() 二、设计一个复利计算函数 invest(),它包含三个参数:amount(资金),rate(利率),time(投资时间)。输入每个参数后调用函数,应该返回每一年的资金总额。它看起来就应该像这样(假设利率为5%):
def invest(amount,rate,time):
print("principal amount:{}".format(amount))
for t in range(1, time + 1):
amount = amount * (1+ rate)
print("year {}: ${}".format(t,amount))
invest(100, .05, 8)
invest(2000, .025, 5) 三、打印1~100内的偶数 第一步打印出1-100的数
for i in range(1,101):
print(i)
第二步打印出其中的偶数
def even_print():
for i in range(1,101):
if i % 2 == 0:
print(i )
even_print()
综合练习
1.求列表之和
a_list = [1,2,3]
print(sum(a_list)) 2.输出随机数
import random

point1 = random.randrange(1,7)
point2 = random.randrange(1,7)
point3 = random.randrange(1,7) print(point1,point2,point3) 导入一个 random 的内置库,每次打印结果肯定是不一样的,其中 random 中的 randrange 方法使用起来就像是 range 函数一样,两个参数即可限定随机数范围。 3.骰子
import random
def roll_dice(number=3,points=None):
print('<<<ROLL THE DICE!>>>')
if point is None:
points = [ ]
while numbers > 0:
point = random.randrange(1,7)
points.append(point)
numbers = numbers - 1
return points 第2行:创建函数,设定两个默认参数作为可选,numbers --骰子数量,points一一三个骰子的点数的列表;
第3行:告知用户开始摇骰子;
第4-5行:如果参数中并未指定points,那么为points创建空的列表;
第6-9行:摇三次骰子,每摇一次numbers就减1,直至小于等于0时,循环停止;
第10行:返回结果的列表。 将点数化成大小
def roll_result(total):
isBig = 11 <= total <=18
isSmall = 3 <= total <=10
if isBig:
return 'Big'
elif isSmall:
return 'Small'
第1行:创建函数,其中必要的参数是骰子的总点数:
第2-3行:设定“大”与“小”的判断标准;
第4-7行:在不同的条件下返回不同的结果。 最后,创建一个开始游戏的函数,让用户输入猜大小,并且定义什么是猜对,什么是猜错,并输出对应的输赢结果。
最后,创建一个开始游戏的函数,让用户输入猜大小,并且定义什么是猜对,什么是猜错,并输出对应的输赢结果。
def start_game( ):
print('<<< GAME STARTS!>>>')
choices = ['Big','Small']
your_choice = input('Big or Small:')
if your_choice in choices:
points = roll_dice( )
total = sum(points)
youWin = your_choice == roll_result(total) if youWin:
print('The points are',points,'You win !')
else:
print('The points are',points,'You lose!')
else:
print('Invalid Words')
start_game( )
start_game( ) 第1行:创建函数,并不需要什么特殊参数;
第2行:告知用户游戏开始;
第3行:规定什么是正确的输入;
第4行:将用户输入的字符串储存在your_choice中
第5、13-15行:如果符合输入规范往下进行,不符合这告知用户并重新开始
第6行:调用roll_dice函数,将返回的列表命名为points;
第7行:点数求和;
第8行:设定胜利的条件——你所选的结果和计算机生成的结果是一致的;
第9-12行:成立则告知胜利,反之,告知失败;
第16行:调用函数,使程序运行。 增加个赌钱的判断
# -*- coding: utf-8 -*-

import random
def roll_dice(numbers=3,points=None):
print('<<< ROLL THE DICE! >>>')
if points is None:
points = []
while numbers > 0:
point = random.randrange(1,7)
points.append(point)
numbers = numbers - 1
return points
def roll_result(total):
isBig = 11 <= total <=18
isSmall = 3 <= total <=10
if isBig:
return 'Big'
elif isSmall:
return 'Small'
def start_game( ):
your_money = 1000
while your_money > 0:
print('<<< GAME STARTS!>>>')
choices = ['Big','Small']
your_choice = input('Big or Small:')
if your_choice in choices:
your_bet = int(input('How much you wanna bet? -'))
points = roll_dice( )
total = sum(points)
youWin = your_choice == roll_result(total)
if youWin:
print('The points are',points,'You win !')
print('You gained {},you have {}now'.format(your_bet,your_money+your_bet))
your_money = your_money + your_bet
else:
print('The points are',points,'You lose!')
print('You gained {},you have {}now'.format(your_bet, your_money - your_bet))
your_money = your_money - your_bet
else:
print('Invalid Words')
else:
print('GAME OVER')
start_game( )


python-循环与判断练习题的更多相关文章

  1. python循环,判断及函数

    python中的for循环 #for循环格式(类似Java中的foreach):for 标识符 in 列表名称 : >>> movies = ["movie1", ...

  2. python循环与判断

    学习一门新的语言最重要的就是练习. 一.脚本需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 二.脚本流程图: 写代码之前画个流程图总是好的,可以让你理清思路,避免写着写着 ...

  3. python:while循环语句及练习题

    while循环语句及练习题 Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务.其基本形式为: while 判断条件: 执行语句... ...

  4. Python 循环判断和数据类型

    循环和判断 1.if 形式 if condition_1: statement_block_1 elif condition_2: statement_block_2 else: statement_ ...

  5. Python的if判断与while循环

    1.if判断 Python 编程中 if 语句用于控制程序的执行,基本形式为: if 判断条件: 执行语句 else: 执行语句 Python中使用缩进代替c语言中的大括号,来告诉程序所执行的内容. ...

  6. Python之条件判断和循环(入门4)

    转载请标明出处: http://www.cnblogs.com/why168888/p/6407755.html 本文出自:[Edwin博客园] Python之条件判断和循环 1. Python之if ...

  7. python学习之判断和循环的使用

    作为一个小白运维,工作中常常发现很多东西还是自动化的好一点,所以就想到的用python来编写脚本.当然,我肯定是不会的啦,哈哈哈~~~~所以啦,身为一个懒癌晚期的上班族不得不在闲余时间来好好学学pyt ...

  8. python之条件判断、循环和字符串格式化

    1. python的条件判断:if和else 在条件判断中可以使用算数运算符 等于:== 不等于:!= 大于:> 小于:< 大于等于:>= 小于等于:<= 示例1: usern ...

  9. Python——3条件判断和循环

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  10. JavaScript 循环判断练习题

    JavaScript 循环判断练习题 小明有一组水果("苹果","梨子","香蕉","葡萄","西瓜" ...

随机推荐

  1. 一个巧妙的方法实现elementUI的table的行选中

    问题背景:点击上面的框,选中下面对象的行数据 刚开始考虑使用的是table的事件:toggleRowSelection,但是发现一个奇怪的现象 <div v-if="orderData ...

  2. PHP实战 新闻管理系统 使用到了bootstrap框架

    刚刚接触 PHP 仿照视频 写了个新闻管理系统 当中也使用到了bootstrap框架 写下来整理一下思路. 这是个非常easy的系统.首先是建立数据库表. mysql>create databa ...

  3. RHEL7系统修复rm -rf /boot /etc/fstab

    RHEL7/Centos7系统发布这么长时间了,大家都知道这个系统的一个特点就是用systemctl代替了init系统守护进程,系统越来越模块化了.在新版的系统中许多的命令也发生了改变,grub也变为 ...

  4. Jmeter-Maven-Plugin高级应用:Modifying Properties

    Modifying Properties Pages 12 Home Adding additional libraries to the classpath Advanced Configurati ...

  5. hdu-悼念512汶川大地震遇难同胞——珍惜现在,感恩生活

    http://acm.hdu.edu.cn/showproblem.php?pid=2191 Problem Description 急!灾区的食物依然短缺! 为了挽救灾区同胞的生命,心系灾区同胞的你 ...

  6. An Easy Task(简箪题)

    B. An Easy Task Time Limit: 1000ms Case Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO f ...

  7. ES6 async await 面试题

    转自:https://juejin.im/post/5c0397186fb9a049b5068e54 1.题目一 async function async1(){ console.log('async ...

  8. SCSS 实用知识汇总

    1.变量声明 $nav-color: #F90; nav { //$width 变量的作用域仅限于{}内 $width: 100px; width: $width; color: $nav-color ...

  9. 移动端网页宽度值(未加meta viewport标签)

    移动端网页宽度值(未加meta viewport标签): iphone:980px Galaxy(盖乐世):980px Nexus:980px blackberry(黑莓):980px LG:980p ...

  10. 腾讯地图api将物理地址转化成坐标

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...