猫史档案馆


【Python作品分享】回文奥妙【作业帖】

用户:东部兔仔-德邦东部兔仔-德邦查看:0 回复:0 评论:0 创建时间:2022-07-22T19:13:39


【作品展示】

center_image

 

【作品介绍】

有手......就......行!

 

【作品源代码】

import random
import string
letter=string.ascii_lowercase
#随机生成字符串
n=random.randint(5,20)
strings=""
for i in range(n):
    char=random.choice(letter)
    strings=strings+char
str_list=list(strings)
print("回文字符串游戏")
round=1
while True:
    print("==============================")
    print("第{}回合".format(round))
    #抽取字符
    print("现在的字符串是:{}".format(str_list))
    your=input(">> 请选择你要拿走的字符:")
    if your in str_list:
        id=str_list.index(your)
        str_list.pop(id)
        print(">> 你选择拿走的字符是:{}")
        print("你抽取后:{}".format(str_list))
    else:
        print("你选择拿走的字符被吃了,请重新选择!")
        continue
    #判断回文字符串
    odd_count=0
    char_count=[]
    for char in str_list:
        if char not in char_count:
            char_count.append(char)
            num=str_list.count(char)
            if num%2==1:
                odd_count=odd_count+1
    if odd_count<=1:
        print(">> 你赢了!")
        break
    print("==============================")
    print("第{}回合".format(round))
    #抽取字符
    print("现在的字符串是:{}".format(str_list))
    your=input(">> 请选择你要拿走的字符:")
    id=str_list.index(your)
    str_list.pop(id)
    print(">>  💻 选择拿走的字符是:{}")
    print(" 💻 抽取后:{}".format(str_list))
    #判断回文字符串
    odd_count=0
    char_count=[]
    for char in str_list:
        if char not in char_count:
            char_count.append(char)
            num=str_list.count(char)
            if num%2==1:
                odd_count=odd_count+1
    if odd_count<=1:
        print(">>  💻 赢了!")
        break
    round+=1

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页