> 文章列表 > 牛客网Python入门103题练习|(06--条件语句(1))

牛客网Python入门103题练习|(06--条件语句(1))

牛客网Python入门103题练习|(06--条件语句(1))

⭐NP43 判断布尔值

描述

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

输入描述:

输入0 或者 1。

输出描述:

输出"Hello World!"或者"Erros!"。

示例1

输入:

1

输出:

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

⭐NP44 判断列表是否为空

描述

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

否则使用print()语句一行输出字符串'my_list is not empty!'。

输入描述:

输出描述:

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

my_list = []
if my_list==[]: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 = ['Niuniu','Niumei','GURR','LOLO']
new_users = ['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
new_current_users = []
for i in current_users:new_current_users.append(i.upper())
for item in new_users:if item.upper() in new_current_users:print('The user name %s has already been registered! Please change it and try again!'%item)else:print('Congratulations, the user name %s is available!'%item)