Python - 基础语法编程练习题

1.实现用户输入用户名和密码,当用户名为 xiaoming 且密码为 123 时,显示登陆成功,否则登陆失败;

import getpass
s_username = "xiaoming"
s_password = "123"

i_username = input("请输入您的用户名:").strip()
i_password = getpass.getpass("请输入您的密码:")

if i_username == s_username and i_password == s_password:
    print("{} 登录成功,欢迎您!".format(i_username))
else:
    print("登录失败,无效的用户名或密码!")

2.实现用户输入用户名和密码,当用户名为 xiaoming 且密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次;

import getpass
s_username = "xiaoming"
s_password = "123"

count = 0
while count < 3:
    i_username = input("请输入您的用户名:").strip()
    i_password = getpass.getpass("请输入您的密码:")

    if i_username == s_username and i_password == s_password:
        print("{} 登录成功,欢迎您!".format(i_username))
        break
    else:
        print("登录失败,无效的用户名或密码!")
    count += 1

3.实现用户输入用户名和密码,当用户名为 xiaoming 或 xiaohua 且密码为 123 时,显示登陆成功,否则登陆失败,失败时允许重复输入三次;

import getpass
s_username_a = "xiaoming"
s_username_b = "xiaohua"
s_password = "123"

count = 0
while count < 3:
    i_username = input("请输入您的用户名:").strip()
    i_password = getpass.getpass("请输入您的密码:")

    if i_username == s_username_a or i_username == s_username_b \
            and i_password == s_password:
        print("{} 登录成功,欢迎您!".format(i_username))
        break
    else:
        print("登录失败,无效的用户名或密码!")
    count += 1

4.使用 while 循环实现输出 1,2,3,4,5,7,8,9,11,12;

count = 0
while count < 12:
    count += 1
    if count == 6 or count == 10:
        continue
    print(count)

5.使用 while 循环输出 100-50 ,从大到小,如100,99,98…,到50时再从0循环输出到50,然后结束;

count = 101
while count > 50:
    count -= 1
    print(count)
    if count == 50:
        count = 0      
        while count <= 50:
            print(count)
            count += 1
        break

6.使用 while 循环实现输出 1-100 内的所有奇数;

count = 1
while count < 101:
    if count % 2 == 1:
        print(count)
    count += 1

7.使用 while 循环实现输出 1-100 内的所有偶数;

count = 1
while count < 101:
    if count % 2 == 0:
        print(count)
    count += 1

8.制作趣味模板程序,需求:
等待用户输入名字、地点、爱好,根据用户的名字和爱好进行任意显示:
如:可爱的xxx,最喜欢在xxx地方干xxx。

i_name = input("请输入您的名字:").strip()
i_place = input("请输入一个地名:").strip()
i_hobby = input("请输入您的爱好:").strip()
print("可爱的 {s_name},最喜欢在 {s_place} 地方干 {s_hobby}。"
      .format(s_name=i_name, s_place=i_place, s_hobby=i_hobby))

9.输入一年份,判断该年份是否是闰年并输出结果。
注:凡符合下面两个条件之一的年份是闰年。
a. 能被4整除但不能被100整除;
b. 能被400整除;

year = input("请输入年份:")

if year.isdigit() == 0:
    print("年份是由数字组成的,这是常识。")
else:
    leap_year = int(year)

    if leap_year % 4 == 0 and leap_year % 100 != 0 or leap_year % 400 == 0:
        print("{} 年是闰年。".format(leap_year))
    else:
        print("{} 年不是闰年。".format(leap_year))

10.假设一年期定期利率为 3.25% ,计算一下需要过多少年,一万元的一年定期存款连本带息能翻番?

interest_rate = 0.0325
principal = 10000
year = 0
total_interest = 0
total_amount = 0
one_year_interest = principal * interest_rate    # 一年的利息

while total_interest <= 10000:
    year += 1
    total_interest = year * one_year_interest

total_amount = total_interest + principal
print("存款 {0} 年后实现连本带息翻番,总金额是 {1} 元。".format(year, total_amount))

11.使用 while ,完成以下图形的输出;
在这里插入图片描述

i = 0
j = 4
while i < 5:
    i += 1
    print("* " * i)
else:
    while j > 0:
        j -= 1
        print("* " * j)
在这里插入代码片

12.根据销售额给员工发提成,提成为阶梯制,假设一个销售人员基本工资为3000元,每月业绩低于5万元,无提成;
5万至10万,提成3%;
10万至15万,提成5%;
15万-25万,提成8%;
25万至35万,提成10%;
35万以上,提成15%;
从键盘获取用户当月业绩,计算其工资+提成的总额。total_wages

basic_salary = 3000
total_wages = 0
value_of_sales = int(input("请输入当月的销售额:").strip())

if value_of_sales < 50000:
    total_wages = basic_salary
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

elif value_of_sales >= 50000 and value_of_sales <= 100000:
    total_wages = basic_salary + value_of_sales * 0.03
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

elif value_of_sales > 100000 and value_of_sales <= 150000:
    total_wages = basic_salary + value_of_sales * 0.05
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

elif value_of_sales > 150000 and value_of_sales <= 250000:
    total_wages = basic_salary + value_of_sales * 0.08
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

elif value_of_sales > 250000 and value_of_sales <= 350000:
    total_wages = basic_salary + value_of_sales * 0.1
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

else:
    total_wages = basic_salary + value_of_sales * 0.15
    print("当月的销售额是 %.2f 元,您的工资是 %.2f 元。" % (value_of_sales, total_wages))

13.地铁交通价格调整为:6公里(含)内3元;6公里至12公里(含)4元;
12公里至22公里(含)5元;
22公里至32公里(含)6元;
32公里以上部分,每增加1元可乘坐20公里。
使用市政交通一卡通刷卡乘坐轨道交通,每自然月内每张卡支出累计满100元以后的乘次价格给予8折优惠;
满150元以后的乘次给予5折优惠;
假设每个月,小明都需要上20天班,每次上班需要来回1次,即每天需要乘坐2次同样路线的地铁。
从键盘获取距离,帮小明计算每月的总花费。

kilometre = int(input("请输入每次做地铁的公里数:").strip())

if kilometre <= 6:
    fare = 3
elif 6 < kilometre <= 12:
    fare = 4
elif 12 < kilometre <= 22:
    fare = 5
elif 22 < kilometre <= 32:
    fare = 6
else:
    fare = 6 + (kilometre - 32) // 20 + 1

total_fare = fare * 2 * 20

if total_fare < 100:
    discount_fare = total_fare
elif 100 <= total_fare < 150:
    discount_fare = (total_fare - 100) * 0.8 + 100
else:
    discount_fare = (total_fare - 150) * 0.5 + 150

print("当月乘坐地铁总共花了 %s 元。" % discount_fare)

14.一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

height = 100
height_list = [100]
count = 0
while count < 10:
    height /= 2
    height_list.append(height)
    count += 1
print("第 10 次落地时总共高度是 %s 米," % sum(height_list),
      "第 10 次反弹高度是 %s 米。" % height)

猜你喜欢

转载自blog.csdn.net/lipandeng_acb/article/details/90760690