Python编程题每日一练day1(附答案)

👉Python编程题每日一练day2(附答案)

提高编程能力的最有效办法就是👉敲代码

题目一: 游乐园的门票

问题描述:
某游乐园院按照游客身高段收取票价:不到 1.0米 的游客免费; 1.0~1.2 米的游客为 80 元;超过 1.2 米的游客为 150 元。
请编写一个死循环,每次循环开始先使用print()语句一行输出字符串"Please tell me your height!\nEnter ‘quit’ to end the program."。
如果读取到的字符串等于’quit’,则使用 break 语句退出循环;
否则将字符串转成浮点数,如果小于1.0米,则使用print()语句一行输出字符串’Your admission cost is 0 yuan.‘,
如果大于等于1.0米且小于等于1.2米,则使用print()语句一行输出字符串’Your admission cost is 80 yuan.’,
如果大于1.2米,则使用print()语句一行输出字符串’Your admission cost is 150 yuan.‘,
然后本次循环结束,再次进入 while 循环中的条件测试。
输入描述:
保证每一行的输入只有浮点数或字符串’quit’,且保证数字合法,范围在[0, 3]。
输出描述:
按题目描述进行输出即可。
示例1
输入:
0.5
1.2
quit

答案:
解法一

while True:
    try:
        print("Please tell me your height!\nEnter 'quit' to end the program.")
        a = input()
        if a == 'quit':
            break
        elif float(a) < 1.0:
            print('Your admission cost is 0 yuan.')
        elif 1.0 < float(a) <= 1.2:
            print('Your admission cost is 80 yuan.')
        else:
            print('Your admission cost is 150 yuan.')
    except:
        break

解法二

operators_dict = {'<': 'less than','==': 'equal'}
print('Here is the original dict:')

for k in sorted(operators_dict):
    print(f'Operator {k} means {operators_dict[k]}.')
print()    

operators_dict['>'] =  'greater than'
print('The dict was changed to:')

for k in sorted(operators_dict):
    print(f'Operator {k} means {operators_dict[k]}.')

题目二:寻找被污染的字符串

小明有一份字符串my_str = ‘I am$ N i u n i u iuniu iuniu, an$d I a m s t u am stu amstudying in N o w Now Nowcoder!’ 被污染了,其中出现了很多奇怪的 符号。现请你使用 s p l i t 函数将这份字符串从符号 符号。 现请你使用split函数将这份字符串从符号 符号。现请你使用split函数将这份字符串从符号处分割成众多字符列表,记录在my_list中,并使用print函数直接打印my_list中的结果。
然后再使用join函数将my_list中的分段字符串重新连接成一个新的字符串,并使用print函数输出。
解法一

my_str = 'I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!' 
my_list=my_str.split('$')
print(my_list)
my_str_new=''.join(my_list)
print(my_str_new)

解法二

a = ("I am$ N$iuniu$, an$d I $am stu$dying in $Now$coder!").split("$");
print(a) 
print("".join(a))

题目三:实现计算求最大公约数和最小公倍数的函数。

答案:

def gcd(x, y):
    (x, y) = (y, x) if x > y else (x, y)
    for factor in range(x, 0, -1):
        if x % factor == 0 and y % factor == 0:
            return factor
def lcm(x, y):
    return x * y // gcd(x, y)

题目四:实现判断一个数是不是素数的函数。

def is_prime(num):
    for factor in range(2, num):
        if num % factor == 0:
            return False
    return True if num != 1 else False

题目五:输入两个正整数,计算最大公约数和最小公倍数。

"""
输入两个正整数计算最大公约数和最小公倍数

Version: 0.1
Author: 编程ID
Date: 2022-06-14
"""

x = int(input('x = '))
y = int(input('y = '))
if x > y:
    x, y = y, x
for factor in range(x, 0, -1):
    if x % factor == 0 and y % factor == 0:
        print('%d和%d的最大公约数是%d' % (x, y, factor))
        print('%d和%d的最小公倍数是%d' % (x, y, x * y // factor))
        break

owcoder.com/link/pc_csdncpt_bcid_python)

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程ID

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值