1,从键盘获取自己的年龄,判断是否大于或者等于18岁,如果满足就输出“已成年!"
def is_int(is_input):
while True:
try:
value = int(input(is_input))
return value
except ValueError:
print("错误,请输入整数!")
age = is_int("请输入年龄:")
if age>=18:
print("已成年")
else:
print("未成年!")
2,定义一个变量存储用户名,定义一个变量存储输入的密码,再判断是否能成功登录
login_name = "abc"
login_pwd = "123"
log_name = input("请输入用户名:")
log_pwd = input("请输入密码:")
def user_login(name,pwd):
if log_name == login_name and log_pwd == login_pwd:
return True
else:
return False
if user_login(log_name,log_pwd):
print("登录成功!")
else:
print("登录失败!")
3,判断一个值,如果为0或者是1则是女
def is_int(is_input):
try:
value = int(input(is_input))
return value
except ValueError:
print("请输入整数!")
a = is_int("请输入一个数:\n")
def battle(a):
if(a == 0) or (a == 1):
return True
else:
return False
if battle(a):
print("女")
else:
print("未匹配到!")
4,if语句
#判断一个值,如果为0则是女;不为0则是男,not
a = 1
if not a:
print("女")
else:
print("男")
#从键盘输入身高,如果身高没有超过150cm,则进动物园不用买票,否则需要买票
a = int(input())
if(a <=150):
print("不需要买票1")
else:
print("需要买票!")
# 输入月份: 234,春天;567夏天;89 10秋天;11 12 1冬天
a = int(input("输入1-12整数:\n"))
if(a == 2) or (a == 3) or (a == 4):
print("您输入的月份:%d是春天" % a )
elif (a == 5) or (a == 6) or (a == 7):
print("夏天")
elif (a == 8) or (a == 9) or (a == 10):
print("秋天")
elif (a == 11) or (a == 12) or (a == 1):
print("冬天")
else:
print("输入错误!")
a = int(input("输入1-12月份,判断季节:"))
if(a > 2) and (a <= 4):
print("春天")
elif (a >= 5) and (a <= 7):
print("夏天")
elif (a >= 8) and (a <= 10):
print("秋天")
elif (a >= 11) and (a <= 12) or (a == 1):
print("冬天")
5,情节描述:上公交车,输入公交卡余额,只要超过2元,就可以上公交车;有空座位,就可以坐下
import random
id_card = int(input("输入公交车的余额:\n"))
car_chair = random.randint(0,24) #使用随机数,0-24个数
if id_card >= 2:
print("可以上公交车")
if car_chair >0:
print("有%d个空座位" % car_chair)
else:
print("没有空座位!")
else:
print("余额不足")
6,for循环
# 范围标识 range()
for a in range(0, 5): #0-5 0,1,2,3,4 --for循环五次
print("第%d次" % (a+1))
name = "你好,我是熊猫!"
for a in name: #在变量name中for循环
print(a)
if a == "是": #如果循环到"是",则输出 "可爱的"
print("可爱的")
7,while循环
#循环5次
"""
i = 0
while i<5: #0-5,0,1,2,3,4 --循环5次
print("当前是第%d次执行循环" % (i+1))
print("i=%d" % i)
i += 1 #循环一次,i+1
"""
#求出1-100相加的和
"""
i = 1
sum = 0
while i<101:
sum = sum+i #0+1 2+2
i +=1 #i=i+1
print("1-100相加的和:",sum)
"""
#求出1-100之间的偶数之和
"""
i = 1
sum = 0
while i<101:
sum = sum+i
i=i+2
print("1-100之间的偶数之和:",sum)
"""
i = 1
sum = 0
while i<101:
if i % 2 == 0: #i%2的余数==2,则就是偶数 --2/2=1余0
sum = sum+i
i=i+1
print("1-100之间的偶数之和:",sum)
8,停止循环
break --跳出循环,不再执行代码
continue --中止本次循环,执行下一次循环
#break:跳出循环,不再执行
"""
i=0
while i<5:
print(i)
if i == 3: #当i==3,则终止整个循环,不再执行
break
i+=1
for i in range(0,5):
print(i)
if i == 3:
break
i+=1
"""
#continue:只结束本次循环,进行下一次的循环
"""
i=0
while i<10:
i += 1 #0+1=1
if i%2 == 0: #如果i/2的余数=0,则终止,输出i;
continue
print(i)
"""
for i in range(0,10):
i+=1
if i%2 == 0:
continue
print(i)
9,与系统玩石头剪刀布
import random
def is_int(is_input):
value = int(input(is_input))
return value
while True:
try:
a = is_int("请输入:剪刀(0),石头(1),布(2)\n")
if (a >= 0 and a <= 2):
b = random.randint(0, 2) # 随机输出0,1,2
print("玩家:%d" % a, "系统:%d" % b)
if (a == 0) and (b == 2) or (a == 1) and (b == 0) or (a == 2) and (b == 1):
print("获胜,你赢了!\n")
elif a == b:
print("平局!")
else:
print("失败,你输了!\n")
else:
print("输入有误,进行不了玩游戏!\n")
except ValueError:
print("请输入整数!")
10,珠穆朗玛峰(8844.43米,8844430毫米)
假如有一张足够大的纸,厚度是0.1毫米,请问折多少次能够达到珠峰的高度?
high = 8844430
count = 0
paper = 0.1
while (paper <= high):
paper*=2
count+=1
print("需要折叠%d" % count+"次\n折叠的高度%d" % paper)
11,需求:在控制台输出100-999之间所有水仙花数量
何为水仙花数: 153 3*3*3 =27 5*5*5= 125 1*1*1=1 1+27+125=153
4567
4567%10 = 个位的值
4567//10%10 = 十位的数
4567//100%10 = 十位的数
for num in range(100, 1000):
a = num%10
b = num//10%10
c = num//100%10
if ((a**3) + (b**3) + (c**3) == num):
print("水仙花为:%d" % num)
12,输出 * 三角形
i=1
while i <= 5:
j = 1
while j <= i:
print("* ",end="")
j += 1
print()
i += 1
13,九九乘法表
i = 1
while i <= 9:
j = 1
while j <= i:
#%d*%d=%-3d " % (j,i,i*j)
#为什么j要放前面,因为i是行数,而第一列第一个数字是1开始的
print("%d*%d=%-3d " % (j,i,i*j),end="")
#print("%d*%d=%-3d " % (i, j, i * j), end="")
j += 1
print()
i += 1
14,字符串
name =""
name1 =''
name2 = str()
print(name==name1 and name1==name2)
namer="熊猫"
l=len(namer)
print("namer的字符个数是:%d" % l )
标签:name,python,else,int,while,print,input,day002
From: https://blog.csdn.net/2401_85470931/article/details/140045799