百依百随网

【Python刷题篇】——Python入门 06 条件语句

【Python刷题篇】——Python入门 06 条件语句

在这里插入图片描述

🤵‍♂️ 个人主页: @北极的刷题三哈 个人主页

👨‍💻 作者简介:Python领域新星创作者。

📒 系列专栏:《牛客题库-Python篇》

🌐推荐《牛客网》——找工作神器|笔试题库|面试经验|实习经验内推,门条求职就业一站解决

👉 点击链接进行注册学习

在这里插入图片描述


牛客题库《在线编程-Python篇》

  python的语句学习还是得基础知识+自己动手同步进行。如果是刷题python新手,需要找一个可以在线练习得网站,门条我建议你去牛客网多练习。语句


Python入门:06 条件语句 NP43 - NP48

在这里插入图片描述

NP43 判断布尔值

描述
Python的刷题条件语句依靠将运算结果转变成布尔值后进行判断,然后分支,门条如果我们直接判断布尔值会怎么样呢?输入布尔变量,语句使用条件语句判断,刷题如果为真则输出"Hello World!“,门条否则输出"Erros!”。语句

输入描述:
输入0或者 1。刷题

输出描述:
输出"Hello World!“或者"Erros!”。门条

示例1
输入:1
输出:Hello World!

代码:

x = int(input())if x == 1:    print("Hello World!")else:    print("Erros!")

NP44 判断列表是语句否为空

描述
创建一个空列表my_list,如果列表为空,请使用print()语句一行输出字符串'my_list is empty!',否则使用print()语句一行输出字符串'my_list is not empty!'

输入描述:

输出描述:
按题目描述进行输出即可。

代码:

my_list = []if len(my_list) == 0:    print('my_list is empty!')else:    print('my_list is not empty!')    

NP45 禁止重复注册

描述
创建一个依次包含字符串'Niuniu'、'Niumei'、'GURR''LOLO'的列表current_users,再创建一个依次包含字符串'GurR'、'Niu Ke Le'、'LoLo''Tuo Rui Chi’的列表new_users,使用for循环遍历new_users,如果遍历到的新用户名在current_users中,则使用print()语句一行输出类似字符串'The user name GurR has already been registered! Please change it and try again!'的语句,否则使用print()语句一行输出类似字符串'Congratulations, the user name Niu Ke Le is available!'的语句。(注:用户名的比较不区分大小写)

输入描述:

输出描述:
按题目描述进行输出即可。

The user name GurR has already been registered! Please change it and try again!Congratulations, the user name Niu Ke Le is available!The user name LoLo has already been registered! Please change it and try again!Congratulations, the user name Tuo Rui Chi is available!

代码:

#为了不区分大小写把判断的都转成大写current_users=[i.upper() for i in ['Niuniu','Niumei','GURR','LOLO']]#因为输出的字符串要保持原来的样子,这个列表内容不能改new_users=['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']for i in new_users:    # 通过upper()来调整大写后再判断    if i.upper() in current_users:          print(f"The user name { i} has already been registered! Please change it and try again!")    else:        print(f"Congratulations, the user name { i} is available!")

NP46 菜品的价格

描述
牛客食堂今天准备了很多丰盛的午餐, 'pizza'10块钱一份,'rice'2块钱一份,'yogurt'5块钱一份,剩下的其他菜品都是8块钱一份。牛牛在某窗口点餐,请你根据他输入的字符串,使用if-elif-else语句判断牛牛需要花费多少钱?

输入描述:
输入一个字符串表示菜品。

输出描述:
输出该菜品的价格。

示例1
输入:pizza
输出:10

代码:

s = input()if s == 'pizza':    print(10)elif s == 'rice':    print(2)elif s == 'yogurt':    print(5)else:    print(8)

NP47 牛牛的绩点

描述
牛牛在门头沟大学学习,一学年过去了,需要根据他的成绩计算他的平均绩点,假如绩点与等级的对应关系如下表所示。请根据输入的等级和学分数,计算牛牛的均绩(每门课学分乘上单门课绩点,求和后对学分求均值)。

A4.0B3.0C2.0D1.0F0

输入描述:
连续输入一行等级一行学分,遇到等级为False则结束输入。

输出描述:
均绩保留两位小数。

代码:

d = { 'A': 4.0, 'B': 3.0, 'C': 2.0, 'D': 1.0, 'F': 0}s = 0c = 0while True:    grade = input()    if grade == "False":        break    credit = int(input())    c += credit    s += d.get(grade)*credit    print('{ :.2f}'.format(s/c))      

NP48 验证登录名与密码

描述
牛客网的登录系统需要验证用户名与密码,当二者都正确时才允许登录,其中管理员的用户名为'admis',密码为'Nowcoder666'。请你使用if-else语句,根据输入的用户名ID和密码,判断该用户等否登录。

输入描述:
第一行输入字符串表示用户名;
第二行输入字符串表示密码。

输出描述:
登录成功输出"Welcome!",登录失败输出"user id or password is not correct!"

示例1
输入:admis
   Nowcoder666
输出:Welcome!

代码:

id = input()password = input()if id == 'admis' and password == 'Nowcoder666':    print('Welcome!')else:    print("user id or password is not correct!")

自测运行:
在这里插入图片描述

保存提交:
在这里插入图片描述


推 荐:牛客题霸-经典高频面试题库

🌐 找工作神器-|笔试题库|面试经验|大厂面试题👉 点击链接进行注册学习
在这里插入图片描述


未经允许不得转载:百依百随网 » 【Python刷题篇】——Python入门 06 条件语句