Python变量字符练习1
Posted UTHN_B
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python变量字符练习1相关的知识,希望对你有一定的参考价值。
1、判断一个变量是否合法:(变量由字母、数字下划线组成;且开头不是数字)
while True:
s = raw_input("please input values:")
if s=="exit":
print "welcome use nexttime!"
break
if s[0].isalpha() or s[0] == "_":
for i in s[1:]:
if not(i.isalnum() or i =="_"):
print "%s变量名不合法!" %s
break
else:
print "%s变量名合法!" %s
二、输入的单词逆向输出(小米笔试)
Sentenct = raw_input("please input sentenct:")
a = Sentenct.split(" ")
print " ".join(a[::-1])
三、输入两个字符串,删除第二个字符串中的所有字符(例:输入:hello world 输出:world hello)
char1 = raw_input("please input first string:")
char2 = raw_input("please input second string:")
for i in char1:
if i in char2:
continue
else:
print "%s" % i,
四、用户输入一个整型数,求该数的阶乘
num = int(raw_input("请输入整型数:"))
rest = 1
for i in range(1, num + 1):
rest *= i
print "输出%d的阶乘:%d" % (num, rest)
五、用户登陆,且不超过三次
for i in range(3):
username = raw_input("please input username:")
password = raw_input("please input password:")
if username == "root" and password == "westos":
print "login success!!"
break
else:
print "login error!!"
print "you have %d times changes" %(2-i)
else:
print "more than three times! please try again after 10s"
六、判断一个学生的缺勤记录(不超过一个A且没有两个来连续的L有奖励)
while True:
Record = raw_input("请输入该学生的缺勤记录:")
print Record.count("LLL") == 0
if Record == "exit":
print "退出。。。"
break
if (Record.count("A") <= 1 and Record.count("LLL") == 0):
print "有奖励!"
else:
print "没有奖赏!!"
七、输入一行字符,统计其中有多少个单词,每两个单词之间以空改革隔开
Line = raw_input("please input sentence:")
li = Line.split()
b = len(li)
print "There are %d words in the line" %(b)
八、求列表中的字符个数
char = raw_input("please input sentence:")
print len(char)
九、是否是回文数
num = raw_input("please input values:")
if (num == num[::-1]):
print "是回文数!"
else:
print "不是回文数"
十、求最大公约数和最小公倍数
min_num = min(num1, num2)
for i in range(1, min_num + 1):
if (num1 % i == 0 and num2 % i == 0):
gys = i
gbs = num1 * num2 / gys
print "%d和%d的最大公约数为:%d" % (num1, num2, gys)
print "%d和%d的最大公为:%d" % (num1, num2, gbs)
十一、猜数字游戏
import random
Systom = random.randint(1, 100)
print Systom
num = 1
while (num <= 5):
Guest = int(raw_input("请输入您猜测的数字(1-100):"))
if Guest < Systom:
print "too small!!"
elif Guest > Systom:
print "too big!!"
else:
print "congratulations!!"
break
num += 1
十二、仿照终端执行操作
import os # 可以执行系统的命令
while True:
cmd = raw_input("[[email protected] ~]$")
if cmd:
if cmd == ‘exit‘:
print "logout"
break
else:
#print ‘run %s‘ %cmd
os.system(cmd)
else:
continue
以上是关于Python变量字符练习1的主要内容,如果未能解决你的问题,请参考以下文章