python字符串/列表基础题

#!/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))
            

  • 10
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值