欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python 02

程序员文章站 2022-04-28 13:31:32
...

1.判断用户输入的变量名是否合法:

#!/usr/bin/env python
import string

name = raw_input("name:")
if name[0] in string.letters+"_":
        for i in name[1:]:
                if i not in string.letters + "_" + string.digits:
                        print "name is wrong!!!"
                        exit(0)
        print "name is right"
else:
        print "name is wrong,please input right name!!!"

python 02

2.用户输入一个数字,判断是否为质数

#!/usr/bin/env python
num = input("num:")
if num > 1:
        for i in range(2,num):
                if (num % i) == 0:
                        print (num,"is not zhishu")
                        break
        else:
                print (num,"is zhishu")
else:
        print (num,"is not zhishu")

python 02

思维脑图链接:
这里写链接内容