if语句学习课堂练手习题

2018-06-18 02:12:43来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

第一次开通自己的博客,想写点什么,因为是python刚入门,就贴一个学习到if语句时练习的几个敲代码的习题




# if语句学习课堂敲键盘


#登录系统:
# user_name="admin"
# password="123"
# name = input("请输入您的用户名:")
# psw = input("请输入您的密码:")
# if name == user_name and psw == password:
# print("恭喜您登录成功!")
# else:
# print("您输入的账号或密码有误,请重新输入!")
#
#

#带访客版的登录系统
# user_name="admin"
# password="123"
# name = input("请输入您的用户名:")
# psw=input("请输入您的密码:")
# if name == user_name and psw == password:
# print("恭喜您登录成功!")
# elif name==user_name:
# print("恭喜您登录成功,但您的访客身份")
# else:
# print("您输入的密码有误,请重新输入!")


# %s 用法
# username = 'admin'
# password = '123'
# user_input = input('please input your username: ')
# pass_input = input('please input your password: ')
# if user_input == username and pass_input == password:
# print ('welcome %s login' % user_input)
# else:
# print ('your username or password is wrong!')


# 判断生肖年份

'''
假设2000年是龙年
输入一个年份,判断是什么年
'''

# year = int(input("请输入一个年份:"))
# if year % 12 == 8:
# nihao = "龙年"
# elif year % 12 == 9:
# nihao = "蛇年"
# elif year % 12 ==10:
# nihao ="马年"
# elif year % 12 == 11:
# nihao = "羊年"
# elif year % 12 ==0:
# nihao = "猴年"
# elif year % 12 ==1:
# nihao = "鸡年"
# elif year % 12 ==2:
# nihao = "狗年"
# elif year % 12 == 3:
# nihao = "猪年"
# elif year % 12 ==4:
# nihao = "鼠年"
# elif year % 12 == 5:
# nihao = "牛年"
# elif year % 12 ==6:
# nihao = "虎年"
# else:
# nihao = "兔年"
# print("您查询的%s年是%s"%(year,nihao))


# 课堂实例:彩票机
# # 随机生成两位数作为中奖号码
# 用户输入一个两位数
# 如果全等于中奖号码,为一等奖
# 如果数字和中奖数字相同,为二等奖
# 如果数字只有一个相同,三等奖
# 否则没有中奖
#
# import random
# luck_num =random.randint(10,90)
# print("本期中奖号码为%s"%luck_num)
#
# num1 = luck_num // 10
# num2 = luck_num % 10
#
# num = int(input("请输入一个两位数"))
# num3 = num // 10
# num4 = num % 10
#
# if num == luck_num:
# print("恭喜您获得一等奖,奖品是:笔记本一个")
# elif num1 == num4 and num2 == num3:
# print("恭喜您获得二等奖,奖品是:笔一支")
# elif num1 == num3 or num1 ==num4 or num2 == num3 or num2 == num4 :
# print("恭喜您获得三等奖,奖品是:头发一根")
# else:
# print("很抱歉,您没有中奖!谢谢参与!")


# while 语句联系
# count = 1
# while count < 10000: #while后跟的是借结束条件(退出循环的条件)
# count += 1
# print("你好啊靓仔%s"%count)
# print("怎么会显示这个")


#while语句课堂练习
# # 猜字游戏,猜大小
# import random
# num1 = random.randint(1,1000)
# num2 = None
# print(num1)
# while num2 != num1:
# num2 = eval(input("输入一个您猜个数字"))
# if num2 == -1:
# break
# if num2 < 1 or num2 >1000:
# print("您输入的数字超出范围,请重新输入!")
# continue
# elif num1 <num2:
# print("猜大了")
# elif num1 > num2:
# print("猜小了")
# else:
# print("猜对了")
# print("游戏结束")

# 计算1-100的总和
# 用while表达
# i = 0
# num = 0
# while i <= 100:
# num = num + i
# i += 1
# print (num)


# 用for循环表达
# j=0
# for i in range (1,101):
# j=j+i
# print(j)

# 99乘法表
# for用法
# m = None
# for m in range(1,10):
# for i in range(1,m+1):
# k = m*i
# if i == m :
# print("%s x %s = %s\t"%(i,m,k))
# else:
# print("%s x %s = %s\t"%(i,m,k),end="")

# while用法
# i=None
# for i in range(1,10):
# j = 1
# while j<=i:
# print("%sx%s=%s"%(j,i,(i*j)),end=' ')
# j+=1
# print()



# m=0
# for i in range(1,10):
# for n in range(1,i+1):
# m=n*i
# print("%s*%s=%s "%(i,n,m),end="\t")
# print("\n")



# 实心正方形
# for a in range(5):
# for b in range(5):
# print("*",end=" ")
# print()

# 空心正方形
# for a in range(5):
# for b in range(5):
# L=[1,2,3]
# if a in L and b in L:
# print(" ",end=" ")
# else:
# print("*", end=" ")
# print()

# 空心正方形标准版
# for i in range(8):
# for j in range(8):
# if i==0 or i==(7) or j==0 or j==(7):
# print("*",end=" ")
# else:
# print(" ",end=" ")
# print()
#


# 空心的三角形(麻烦版)
# for a in range(4):
# for b in range(7):
# if a == 0 and b == 3:
# print("*",end=" ")
# elif a == 1 and b == 2:
# print("*", end=" ")
# elif a == 1 and b == 4:
# print("*", end=" ")
# elif a == 2 and b == 1:
# print("*",end=" ")
# elif a == 2 and b == 5:
# print("*", end=" ")
# elif a == 3:
# print("*",end=" ")
# else:
# print(" ",end=" ")
# print()


# 网络版空心三角形
# i = 0
# while i < 10:
# j = 1
# while j < 20:
# mid = 19 // 2 + 1
# if j ==mid or j ==i +mid or j==mid -i or i ==9:
# print("*",end='')
# else :
# print(" ",end='')
# j +=1
# i +=1
# print("")

# 空心等边三角形标准版
# m = 8
# for i in range(m+1):
# for j in range(m-i):
# print(" ",end="")
# j += 1
# for k in range(0,2*i-1):
# if k == 0 or k == 2*i-2 or i == m:
# if i == m:
# if k % 2 ==0:
# print("* ",end="")
# else:
# print(" ",end="")
# else:
# print("* ",end="")
# else:
# print(" ",end="")
# k += 1
# print("")
# i += 1




# d = {'a': 1, 'b': 2, 'c': 3}
# for i in d:
# print(i)
# for i in d.values():
# print(i)
# for i in d.items():
# print(i)


# 三:女神搜索:
# 自定义一个自己的女神
# 遍历一个姓名列表
# 如果找到自己的女神,那么就打印我找到你了,否则打印我要的不是你
#
# L = ["tom","jerry","jack","rose"]
# name_1 = "rose"
# a= 0
# for a in L:
# print(a)
# if name_1 == a:
# print("女神我找到你了!")
# else:
# print("我找的不是你")
#


# 空心菱形
# for i in range(10):
# m=int(10/2)-1
# if i<m:
# for n in range(m+i+1):
# if (n<(m-i))or(n>(m-i)and(n<(m+i))):
# print(" ",end=" ")
# else:
# print("*",end=" ")
# print()
# if i==m:
# for n in range(2*m+1):
# if n>0and n<(2*m):
# print(" ",end=" ")
# else:
# print("*",end=" ")
# print()
# if i>m:
# for n in range((m+3)*2-i-1):
# if (n<(i-m))or(n>(i-m)and(n<(m+3)*2-i-2)):
# print(" ",end=" ")
# else:
# print("*",end=" ")
# print()




# num = eval(input("请输入一个大于2的整数"))
#
# while True:
# if num > 2 and num%1 == 0:
# break
# else:
# num = eval(input("输入错误,请重新输入一个大于2的整数"))
#
# print("实心直角形")
# for i in range(num):
# for j in range(num):
# if i>=j:
# print("*", end="\t")
# print()
#
# print("空心直角形")
# for i in range(num):
# for j in range(num):
# if i==j or j==0 or i==(num-1):
# print("*", end="\t")
# else:
# print(" ",end="\t")
# print()
#
# print("实心正三角形")
# for i in range(num):
# for j in range(num*2-1):
# if j<(num-i-1) or j>(num+i-1):
# print(" ",end="\t")
# else:
# print("*",end="\t")
# print()
#
# print("实心正三角形2")

# for i in range(num):
# print(" \t"*(num-i-1)+"*\t"*(2*i+1)+" \t"*(num-i-1))
#
# print("空心正三角形")

# for i in range(num):
# for j in range(num*2-1):
# if j<(num-i-1) or j>(num+i-1):
# print(" ",end="\t")
# elif j==(num-i-1) or j==(num+i-1) or i==num-1:
# print("*",end="\t")
# else:
# print(" ", end="\t")
# print()
#
# print("空心正三角形2")


# for i in range(num):
# if i>0 and i<(num-1):
# print(" \t"*(num-i-1)+"*\t"+" \t"*(2*i-1)+"*\t"+" \t"*(num-i-1))
# else:
# print(" \t"*(num-i-1)+"*\t"*(2*i+1)+" \t"*(num-i-1))

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:python—命名规范

下一篇:初学Python——第一节课