第四章练习题

1、猜数游戏。在程序中预设一个0~9之间的整数,让用户通过键盘输入所猜的数,如果大于预设的数,显示“遗憾,太大了”;小于预设的数,显示“遗憾,太小了”,如此循环,直至猜中此数,显示“预测N次,你猜中了!”,其中N是用户输入数字的次数。

prenumber=6
N=0
while True:
    number=eval(input("请输入一个0~9之间的整数:"))
    if number>prenumber:
        print("遗憾,太大了")
        N+=1
    elif number<prenumber:
        print("遗憾,太小了")
        N+=1
    else:
        N+=1
        print("预测{}次,你猜中了".format(N))
        break

2、统计不同字符个数。用户从键盘输入一行字符,编写一个程序,统计并输出其中英文字符、数字、空格和其他字符的个数。

string=input("Please input a string contains char,number,space or other characters:\n")
char,num,spa,other=0,0,0,0
for i in string:
    if "a"<=i<="z" or "A"<=i<="Z":
        char+=1
    elif "0"<=i<="9":
        num+=1
    elif i==' ':
        spa+=1
    else:
        other+=1
print("There are {} chars,{} numbers,{} spaces,{} other characters".format(char,num,spa,other))

注:记得初始化
3、最大公约数计算。从键盘接收两个整数,编写程序求出这两个整数的最大公约数和最小公倍数(提示:求最大公约数可用辗转相除法,求最小公倍数则用两数的积除以最大公约数即可。)

a,b=eval(input("Please input two integer(use , to separate):\n"))
if a!=b:
    if a>b:
        big=a
        small=b
    else:
        big=b
        small=a
    while 1:
        c=big%small
        if c!=0:
            big=small
            small=c
            continue
        else:
            break
else:
    small=a
print("最大公约数为{}".format(small))
print("最小公倍数为{}".format(int(a*b/small)))

另法:(用函数)

def modsf(a,b):
    c=max(a,b)%min(a,b)
    if c==0:
        return min(a,b)
    else:
        return modsf(min(a,b),c)
A,B=eval(input("请输入两个整数(用逗号隔开):"))
LCM,GCD=0,0
GCD=modsf(A,B)
LCM=A*B/GCD
print("最大公约数为{},最小公倍数为{}".format(GCD,LCM))

4、猜数游戏续。改编程序练习题1,让计算机能够随机产生一个预设数字,范围在0~100之间,其他游戏规则不变。

from random import randint
prenumber=randint(0,100)
N=0
while True:
    number=eval(input("请输入一个0~100之间的整数:"))
    if number>prenumber:
        print("遗憾,太大了")
        N+=1
    elif number<prenumber:
        print("遗憾,太小了")
        N+=1
    else:
        N+=1
        print("预测{}次,你猜中了".format(N))
        break

5、猜数游戏续。对于程序练习题4程序,当用户输入的不是整数(如字母、浮点数等)时,程序会终止执行退出。改编该程序,当用户输入出错时给出“输入内容必须为整数!”的提示,并让用户重新输入。

from random import randint
prenumber=randint(0,100)
N=0
while True:
    try:
        number=eval(input("请输入一个0~100之间的整数:"))
        if isinstance(number,int):
            if number>prenumber:
                print("遗憾,太大了")
                N+=1
            elif number<prenumber:
                print("遗憾,太小了")
                N+=1
            else:
                N+=1
                print("预测{}次,你猜中了".format(N))
                break
        else:
            print("输入内容必须为整数!")
            continue
    except NameError:
        print("输入内容必须为整数!")

注意:区别break和continue
补充知识点(isinstance(object,classinfo)):
在这里插入图片描述6、羊车门问题。有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么。参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择。请问:参赛者更换选择后能否增加猜中汽车的机会?——这是一个经典问题。
请使用random库对这个随机事件进行预测,分别输出参赛者改变选择和坚持选择获胜的机率。

from random import randint
trueno = 0 
trueyes = 0 
falseno = 0
falseyes = 0
man = eval(input("Choose one door number from 1, 2, 3:"))
car = randint(1,3)
g = []
for i in [1,2,3]:
    if i != car:
        g.append(i)
for i in g:
    if man != i:
        door = i
print("man:{},car:{},door open:{}".format(man,car,door))
choice = input("Do you what to change your choice?\nInput yes or no:")
if choice == 'no':
    if man == car:
        trueno += 1
    else:
        falseno += 1
else:
    for i in [1,2,3]:
        if i != man and i != door:
            man = i
            break
        else:
            continue
    if man == car:
        trueyes += 1
    else:
        falseyes += 1

print("choice is: {}, guess {}.".format(choice, 'yes' if trueno==1 or trueyes==1 else "no"))
print("man:{},car:{},door open:{}".format(man,car,door))

输出结果:

Choose one door number from 1, 2, 3:1
man:1,car:1,door open:3
Do you what to change your choice?
Input yes or no:no
choice is: no, guess yes.
man:1,car:1,door open:3
  • 20
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值