python逻辑判断多行_Python逻辑判断,循环

阶乘

方法一:利用循环方法 #!/usr/bin/env python

# coding:utf-8

def fact(n):

total = 1

if n == 0:

total = 1

else:

for i in range(1,n+1):

total *= i

return total

while True:

n = input("Please input a int number(n>0): ")

if not n.isnumeric() or int(n) < 0:

print("Please input a number!")

continue

n = int(n)

result = fact(n)

print("{}!={}".format(n,result))

break

方法二:利用闭包函数 #!/usr/bin/env python

# coding:utf-8

def fact(n):

total = 1

if n == 0:

return total

return n * fact(n-1)

while True:

n = input("Please input a int number(n>0): ")

if not n.isnumeric() or int(n) < 0:

print("Please input a number!")

continue

n = int(n)

result = fact(n)

print("{}!={}".format(n,result))

break

统计字符串中字符和数字的个数 #!/usr/bin/env python

# coding:utf-8

status = 1

while status:

string = input("Please input a string including numbers and strings or other special symbols(input 'quit' to quit): ")

if string == "quit":

exit(1)

digit = alpha = space = other = 0

for i in string:

if i.isdigit():

digit += 1

elif i.isalpha():

alpha += 1

elif i.isspace():

space += 1

else:

other += 1

print("数字:%d"%digit)

print("字母:%d"%alpha)

print("空格:%d"%space)

print("其它:%d"%other)

乘法表 #!/usr/bin/env python

# coding:utf-8

for i in range(1,10):

for j in range(1,i+1):

print("{}X{}={}".format(j,i,i*j),end=" ")

print()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值