#!/usr/bin/env python
# coding: utf-8
# In[36]:
#猜数字游戏(豪华修订版)
import random
guess=random.randint(1,100)
num=0
count=0
while(num!=guess):
try:
num=eval(input("请在1-100之间猜一个数字:"))
count+=1
if num>guess:
print("你猜的数字大了!")
continue
elif num<guess:
print("你猜的数字小了!")
continue
except NameError:
print("输入的不是整数!")
else:
print("你猜对了!你一共猜了{}次!".format(count))
# In[38]:
#循环输出字符每次减少字符串一个字符
s="PYTHON"
while s!="":
print(s,end="")
s=s[:-1]
# In[51]:
#循环输出字符每次减少一字符,如果遇到T就退出
s="PYTHON"
while s!="":
for c in s:
if c =="T":
break
else:
print(c,end="")
s=s[:-1]
# In[65]:
#10个随机整数列表,最大值最小值
import random
random.seed(1)
ls=[]
while(len(ls)!=10):
ls.append(random.randint(1,100))
print("列表为{0},列表最大值为{1},最小值为{2}".format(ls,max(ls),min(ls)))
# In[66]:
#空列表循环加10个整数
ls=[]
while(len(ls)!=10):
ls.append(eval(input("请输入数字:")))
print("列表为{0}".format(ls))
# In[69]:
#10个字符串列表转换为大写输出
ls=[]
while(len(ls)!=10):
ls.append(input("请输入字符:"))
result=",".join(ls)
print("列表为{0}".format(result.upper()))
# In[70]:
#10个整数列表把偶数替换成0
import random
random.seed(1)
ls=[]
while(len(ls)!=10):
ls.append(random.randint(1,100))
for i in range(10):
if(ls[i]%2==0):
ls[i]=0
print("列表为{0}".format(ls))
# In[71]:
#10个随机整数,大于等于5替换为1,其余替换为0
random.seed(1)
ls=[]
while(len(ls)!=10):
ls.append(random.randint(1,100))
for i in range(10):
if(ls[i]>=5):
ls[i]=1
else:ls[i]=0
print("列表为{0}".format(ls))
# In[80]:
#10个随机整数,把其中奇数按照从小到大排列
random.seed(1)
ls=[]
p=0
lls=[]
j=0
while(len(ls)!=10):
ls.append(random.randint(1,100))
for i in range(10):
if(ls[i]%2!=0):
lls.append(ls[i])
lls.sort()
print("列表为{0}".format(ls))
print("列表为{0}".format(lls))