练习代码(2)
来源:互联网 发布:光大证券mac版下载 编辑:程序博客网 时间:2024/04/28 13:22
2.1
import easyguieasygui.msgbox("hello,world!")2.2
import easygui as gimport syswhile 1: g.msgbox("welcome") msg =("Do you hunger for knowledge?") title = "game" choices =["coding","sleeping","reading","loving"] choice = g.choicebox(msg,title,choices) g.msgbox("Your choice is "+ str(choice),"result") msg = "restart?" title = "please have your choice" if g.ccbox(msg.title): pass else: sys.exit(0)2.3
import random as rclass Fish: def __init__(self): self.x = r.randint(0,10) self.y = r.randint(0,10) def move(self): self.x -= 1 print("my position is ",self.x,self.y)class A(Fish): passclass B(Fish): passclass C(Fish): passclass D(Fish): def __init__(self):## Fish.__init__(self) super().__init__() self.hungry = True def eat(self): if self.hungry: print("eat them all!") self.hungry = False else: print("that's enough")2.4
import time as tclass Mytimer(): def __init__(self): self.prompt ="未开始计时" self.lasted =[] self.start =0 self.stop =0## def __str__(self):## return self.prompt def start(self): self.start = t.localtime() print("开始计时") def stop(self): self.stop = t.localtime() print("计时结束") def calculate(self): self.lasted =[] self.prompt ="总共运行" for index in range(6): self.lasted.append(self.stop[index]-self.start[index]) self.prompt+= str(self.lasted[index]) print(self.prompt)2.5
def sqrt(x):#判断完全平方数 ans=0 if x>=0: while ans*ans<x: ans+=1 if ans*ans!=x: print(x," ia not a perfect squart") return None else: return ans else: print("x should be positive") return Nonedef solve(legs,heads): for chicks in range(0,heads+1):#进行循环 pigs=heads-chicks tot=4*pigs+2*chicks if tot==legs:#寻找到满足限制的条件 return(chicks,pigs) return(None,None)def barnYard(): heads=int(input("enter the number of heads")) legs=int(input("enter the number of legs")) pigs,chicks=solve(legs,heads) if pig == None: print("no solution") else: print("number of pigs",pigs) print("number of chicks",chicks)def solve1(legs,heads): for spiders in range(0,heads+1): for chicks in range(0,heads+1): pigs=heads-chicks tot=4*pigs+2*chicks+8*spiders if tot==legs: return(chicks,pigs,spiders) return(None,None,None)def barnYard1(): heads=int(input("enter the number of heads")) legs=int(input("enter the number of legs")) pigs,chicks=solve1(legs,heads) if pigs == None: print("no solution") else: print("number of pigs",pigs) print("number of chicks",chicks) print("number of spiders",spiders)def solve2(legs,heads): solutionFound = False for spiders in range(0,heads+1): for chicks in range(0,heads+1): pigs=heads-chicks tot=4*pigs+2*chicks+8*spiders if tot==legs: return(chicks,pigs,spiders) print("number of pigs",pigs) print("number of chicks",chicks) print("number of spiders",spiders) solutionFound=True return(None,None,None) if not solutionFound: print("no solution")2.6
def isPalindrome(n):#字符串赋值到n,然后比较回文序列 if len(n)<=1: return True else: return n[0]==n[-1] and isPalindrome(n[1:-1])def isPalindrome1(n,indent): print(indent,"isPalindrome called with",n) if len(n)<=1: print(indent,"about to return True from base case") return True else: ans=n[0]==n[-1]and isPalindrome1(n[1:-1],indent+indent) print(indent,"about to return",ans) print(ans)
0 0
- 练习代码(2)
- 近期代码回归复习练习(2)
- 练习代码(1)
- 练习代码(3)
- 练习代码(三)复用类
- 练习代码(四)多态
- JSP代码练习(四)
- JSP代码练习(五)
- JSP代码练习(六)
- JSP代码练习(七)
- JSP代码练习(八)
- Java 基础一些代码练习笔记(多态2)
- Python基础教程(第2版)练习代码
- 代码练习
- 代码练习
- 练习代码
- 代码练习
- 代码练习
- Spring 的Servlet拦截器匹配规则
- web开发之图片选择
- hdoj 2022
- 错误票据
- hibernate初探之一对多映射(二)
- 练习代码(2)
- 洛谷 P1616 疯狂的采药
- George and Number CodeForces - 387C
- OPTI Toolbox Wiki
- 项目实战(连载):基于Angular2+Mongodb+Node技术实现的多用户博客系统教程(1)
- Struts2方便实用的内置校验器
- 1050. String Subtraction (20)
- 基于CentOS 6.8的Git服务器的搭建
- 米老师交流:教育之美