《python语言程序设计基础》嵩天著-第4章程序练习题答案

练习题代码

正在看书自学中,贴出自己的作业,欢迎大家交流指正,一起进步~

4.1 猜数游戏

在程序中预设一个0~9之间的整数,用户通过键盘输入所猜的数,给出猜测结果,并且猜对是给出猜测总次数。

# 4.1
from random import randint
num = randint(0,9) # 也可以自己随便设置一个 num = 3
n = 1
while 1:
    a = eval(input("Guess a number from 0 to 9: "))
    if a < num:
        print("Too small.")
        n += 1
    elif a > num:
        print("Too large.")
        n += 1
    else:
        print("Congraduations! Guess {:d} time(s)." .format(n))
        break

结果

Guess a number from 0 to 9: 6
Too large.
Guess a number from 0 to 9: 3
Too small.
Guess a number from 0 to 9: 5
Too large.
Guess a number from 0 to 9: 4
Congraduations! Guess 4 time(s).

4.2 统计不同字符个数

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

# 4.2
string = input("Please input a string contains number, char, \
space or other characters:\n")
num, char, 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 {} numbers, {} chars, {} spaces, {} other characters." .format(num, char, spa, other))

结果

Please input a string contains number, char, space or other characters:
123  hduewi 87,./d
There are 5 numbers, 7 chars, 3 spaces, 3 other characters.

4.3 最大公约数计算

从键盘接受2个整数,变成求出最大公约数和最小公倍数。(提示:求最大公约数用辗转相除法,求最小公倍数用量数的乘积除以最大公约数)

辗转相除法
用较大数除以较小数,再用出现的余数(第一余数)去除除数,再用出现的余数(第二余数)去除第一余数,如此反复,直到最后余数是0为止。如果是求两个数的最大公约数,那么最后的除数就是这两个数的最大公约数。

# 4.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("最小公倍数为 {:d}".format(int(a*b/small)))

结果:

Please input two integer (use , to separate):
128,36
最大公约数为 4
最小公倍数为 1152


Please input two integer (use , to separate):
24,24
最大公约数为 24
最小公倍数为 24

补充另一个古老的方法
更相减损术
《九章算术》中介绍了这个方法,“可半者半之,不可半者,副置分母、子之数,以少减多,更相减损,求其等也。以等数约之。”
  第一步:任意给定两个正整数;判断它们是否都是偶数。若是,则用2约简;若不是则执行第二步。
  第二步:以较大的数减较小的数,接着把所得的差与较小的数比较,并以大数减小数。继续这个操作,直到所得的减数和差相等为止。
  则第一步中约掉的若干个2与第二步中等数的乘积就是所求的最大公约数。
  其中所说的“等数”,就是最大公约数。求“等数”的办法是“更相减损”法。所以更相减损法也叫等值算法。

4.4

见4.1

4.5 猜数游戏续

当用户输入不是整数(如字母、浮点数等),程序会中止执行退出。改编4.4的程序,当用户输入错误时给出提示并让用户重新输入。

#4.5
from random import randint
num = randint(0,100) # 也可以自己随便设置一个 num = 3
n = 1
while 1:
    try:
        a = eval(input("Guess a number from 0 to 100: "))
        if isinstance(a,int):

            if a < num:
                print("Too small.")
                n += 1
            elif a > num:
                print("Too large.")
                n += 1
            else:
                print("Congraduations! Guess {:d} time(s)." .format(n))
                break
        else:
            print("Input wrong!")
            continue
    except NameError:
        print("Input wrong!")
    

结果


Guess a number from 0 to 100: 3.6
Input wrong!
Guess a number from 0 to 100: fr
Input wrong!
Guess a number from 0 to 100: 50
Too small.
Guess a number from 0 to 100: 75
Too large.
Guess a number from 0 to 100: 68
Too small.
Guess a number from 0 to 100: 70
Congraduations! Guess 4 time(s).

猜你喜欢

转载自blog.csdn.net/weixin_40406018/article/details/89220101