练习代码(2)

2.1
import easygui

easygui.msgbox("hello,world!")
2.2
import easygui as g
import sys

while 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 r

class 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):
    pass
class B(Fish):
    pass
class C(Fish):
    pass
class 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 t

class 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 None


def 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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值