2019年5月16日,大二下python作业2

猜数字游戏

import random
z=1
flag=0
while z!='exit':
    x=random.randint(1,9)
    z=input("give me a number,or print'exit' to leave the game")
    if z!='exit':
        y=int(z)
        while y!=x:
            if y<x:
                print('you guessed too low')
                y=int(input('give me a number again'))
                flag+=1
                continue
            if y>x:
                print('you guessed too high')
                y=int(input('give me a number again'))
                flag+=1
                continue
        print('you win! you have guessed ',flag,' times','\nnow guess next number')
    if z=='exit':
        break


与平方根公式对比

import math
def square_root():
    e=0.0000001
    x=6.0
    dif=1
    n=1
    while n<=9:
        while e>0:
            dif=abs(x-0.5*(x+n/x))
            x=0.5*(x+n/x)
            if dif<e:
                break
        z=math.sqrt(n)
        c=z-x  
        print('%-10s' % n,'%-20s' % x,'%-20s' % z,'%-26s' % c)
        n+=1.0
square_root()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值