ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

《Python程学设计基础》【第四章】习题

2022-01-20 17:59:20  阅读:168  来源: 互联网

标签:程学 format Python sum random number choice print 习题


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

alb="abcdefghijklmnopqrstuvwxyz"
number="0123456789"
n1,n2,n3,n4=0,0,0,0        #n1、n2、n3分别统计数字、字母、空格和其它字符的个数
str=input("Enter your string:")
for c in str:
    if c in alb:
        n1+=1
    elif c in number:
        n2+=1
    elif c==' ':
        n3+=1
    else:
        n4+=1
print("The {}-character-string has:\n".format(len(str))
      +"{} letters\n".format(n1)
      +"{} numbers\n".format(n2)
      +"{} ' '\n".format(n3)
      +"{} other characters.".format(n4))

#2编写程序,求输入两个整数的最大公约数和最小公倍数(最小公倍数可以通过转转相除法求得)

a=eval(input("Input the 1st number:"))
b=eval(input("Input the 2nd number:"))

div =1
for i in range(2,min(a,b)+1):
    if a%i==0 and b%i==0:
        div=i

mul=int(a*b/div)
print("For {} and {}:".format(a,b)+
      "the greatest common divisor is {}; ".format(div)+
      "the lowest common multiple is {}.".format(mul))

#3猜数问题:让计算机随机生成一个0~100之间的整数。用户从键盘输入一行字符猜测这个数是多少。计算机将两个数比较并给出比较结果,猜错了再猜,如此循环,知道猜对为止。(注:使用异常处理异常字符串)

import random
x=random.randint(0,100)

while True:
    a=input("Enter a number you guess between 0 and 100:")
    try:
        if eval(a)>100 or eval(a)<0:
            print("Your number is out of range! Try again!")
            continue
        else:
            if eval(a)>x:
                print("Your number is too large, try again!")
                continue
            elif eval(a)<x:
                print("Your number is too low, try again!")
            else:
                print("Congratulation! Your number {} is correct!".format(x))
                break
    except TypeError:
        print("Wrong format!")
    except NameError:
        print("Wrong format!")

#4羊车门问题。有3扇关闭的门,一扇门后面停着车,其余门后是山羊,只有主持人知道每扇门后面是什么。参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后参赛者更换自己的选择,参选者若选到汽车则获胜。请问:参赛者更换选择后能否增加猜中汽车的机会?——这是一个经典问题,请使用random库对这个随机事件进行预测,分别输出参赛者改变选择和坚持选择获胜的几率。(要求使用异常处理)

import random
a,b,c="car","goat1","goat2"       #a、b、c代表三扇门
sum=1000000                       #样本量

#不提前开门的情况:
win1=0
for i in range(sum):
    choice=random.choice([a,b,c])
    if choice=='car':
        win1+=1

print("The rate of winning on the 1st condition is {}".format(win1/sum))

#提前开门且改变选择的情况:
win2=0
for i in range(sum):
    choice=random.choice([a,b,c])
    if choice=='car':
        #prdoor=b/c
        choice=c                 #此时选b和c都一样

    elif choice=='goat1':
        #prdoor=c
        choice=a

    else:
        #prdoor=b
        choice=a

    if choice=='car':
        win2+=1
print("The rate of winning on the 2nd condition is {}".format(win2/sum))

输出结果:

The rate of winning on the 1st condition is 0.332045
The rate of winning on the 2nd condition is 0.667128


标签:程学,format,Python,sum,random,number,choice,print,习题
来源: https://blog.csdn.net/weixin_63045396/article/details/122599747

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有