携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第11天
- NP43 判断布尔值
描述
Python的条件语句依靠将运算结果转变成布尔值后进行判断,然后分支,如果我们直接判断布尔值会怎么样呢?输入布尔变量,使用条件语句判断,如果为真则输出"Hello World!",否则输出"Erros!"。
输入描述:
输入0 或者 1。
输出描述:
输出"Hello World!"或者"Erros!"。
示例1
参考代码如下;
bool_num = bool(int(input())) # input()的输入值只有将其转化为int之后,再去转化为bool类型比较才有意义
if(bool_num):
print("Hello World!")
else:
print("Erros!")
注意bool('0'), 字符串无论是'0'或'1'都为True
print(bool('0'))
- 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!')
别人的代码:
#第一种:直接判断
my_list=[]
if my_list:#存在即为真,也就不是空值
print('my_list is not empty!')
else:
print('my_list is empty!')#否则就是空值
第二种:判断长度
my_list=[]
if len(my_list):#长度值不为空就代表有元素
print('my_list is not empty!')
else:#长度值为空代表就是空列表
print('my_list is 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']
for i in new_users:
if i.upper() in current_users:
print('The user name %s has already been registered! Please change it and try again!'% i)
else:
print('Congratulations, the user name %s is available!'% i)
参考别人的代码;
current_users = ['Niuniu','Niumei','GURR','LOLO']
new_users = ['GurR','Niu Ke Le','LoLo','Tuo Rui Chi']
for i in new_users:
for j in current_users:
if i.lower() == j.lower():
print(f'The user name {i} has already been registered! Please change it and try again!')
break
else:
print(f'Congratulations, the user name {i} is available!')
- NP46 菜品的价格
描述
牛客食堂今天准备了很多丰盛的午餐, 'pizza':10块钱一份,'rice' :2块钱一份,'yogurt':5块钱一份,剩下的其他菜品都是8块钱一份。牛牛在某窗口点餐,请你根据他输入的字符串,使用if-elif-else语句判断牛牛需要花费多少钱?
输入描述:
输入一个字符串表示菜品。
输出描述:
输出该菜品的价格。
示例1
参考代码如下:
lunch = ['pizza','rice','yogurt']
n1 = input()
if n1 == lunch[0] :
print("10")
elif n1 == lunch[1]:
print("2")
elif n1 == lunch[2]:
print("5")
else:
print("8")
看到一个非常好的代码:
dish = { 'pizza':10, 'rice':2, 'yogurt':5, 'others':8}
x= input()
if x not in dish.keys():
x = 'others' # 将没有的keys 都归到 others里
for i in dish.keys():
if i==x:
print(dish[i])
break
- NP47 牛牛的绩点
参考代码如下:
score = {'A':4.0,'B':3.0,'C':2.0,'D':1.0,'F':0}
s1 = 0
sum1 = 0
while 1:
x = input()
if x.lower() == 'false':
break
y = int(input())
sum1 += score[x]*y
s1 += y
print('%.2f' %(sum1/s1))
本题对输入进行了条件的限制,即意味着需要对input进行条件限制,这也意味着需要使用循环对input进行设置
- NP48 验证登录名与密码
描述
牛客网的登录系统需要验证用户名与密码,当二者都正确时才允许登录,其中管理员的用户名为'admis',密码为'Nowcoder666'。请你使用if-else语句,根据输入的用户名ID和密码,判断该用户等否登录。
输入描述:
第一行输入字符串表示用户名;
第二行输入字符串表示密码。
输出描述:
登录成功输出"Welcome!",登录失败输出"user id or password is not correct!"
示例1
我的代码:
user_name = input()
code = input()
if user_name == 'admis':
if code == 'Nowcoder666':
print("Welcome!")
else:
print("user id or password is not correct!")
else :
print("user id or password is not correct!")
可参考代码:
userid=input()
password=input()
if userid=='admis' and password=='Nowcoder666':
print('Welcome!')
else:
print("user id&nbs***bsp;password is not correct!")