- 带循环和条件判断的给定一个数值num, 用户输入使用raw_input()函数来提示用户输入一个1和100之间的数,
如果用户输入的数等于num, 显示成功并退出。否则显示一个错误信息然后再次提示用户输入数值,直到满足条件为止。
while 1:
num = int(raw_input("num=:"))
if num >= 1 and num <= 100:
print num
else:
print "输入有误%s" %(num)
2.有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
count = 0
for i in range(1, 5):
for j in range(1, 5):
for x in range(1, 5):
if i != j and 1 != x and j != x:
count +=1
print i*100+j*10+x
print count
3.句子反转
题目描述:
给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如: (1) “hello xiao mi”-> “mi xiao hello”
输入描述:
输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)
输出描述:
对于每个测试示例,要求输出句子中单词反转后形成的句子
s=raw_input("s=:")
z=[]
if len(s)>1000:
print "Error:输入字符串长度超过1000!"
else:
for i in range(len(s)):
if s[i] not in z:
z.append(s[i])
str="".join(z)
print str
s=raw_input("s=:")
if len(s)<1000:
print " ".join(s.split()[::-1])
else:
print "ERROR:%s more than 1000 characters!" %(s)
4.字符集合
- 题目描述:
输入一个字符串,求出该字符串包含的字符集合 - 输入描述:
每组数据输入一个字符串,字符串最大长度为100,且只包含字母,不可能为空串,区分大小写。 - 输出描述:
- 每组数据一行,按字符串原有的字符顺序,输出字符集合,即重复出现并靠后的字母不输出。
示例:
- 输入:
abcqweracb - 输出:
abcqwer
- 输入:
s=raw_input("s=:")
z=[]
if len(s)>100:
print "Error:输入字符串长度超过100!"
else:
for i in range(len(s)):
if s[i] not in z:
z.append(s[i])
str="".join(z)
print str
5.题目描述:
输入两个字符串,从第一字符串中删除第二个字符串中所有的字符。例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”
- 输入描述:
每个测试输入包含2个字符串
- 输出描述:
输出删除后的字符串
- 示例:
输入
They are students.
aeiou
输出
Thy r stdnts.
s1 = raw_input("s1=:")
s2 = raw_input("s2=:")
for i in s1:
for j in s2:
if i == j:
s1=s1.replace(i,"")
print s1
或
a = raw_input("a=:")
b = raw_input("b=:")
for i in a:
for j in b:
if i == j:
a=a.replace(i,"")
print a