m0_62055949 2022-04-29 13:16 采纳率: 91.7%
浏览 100
已结题

python怎么用循环删除字符串的某个字符

img


题目如图

img


怎么保证猜过的词不重复出现?
我想的是每次猜正确后就把这个词从字符串中去除掉,但是不知道代码怎么写,以及while循环的条件是什么,或者有更好的思路吗?

  • 写回答

4条回答 默认 最新

  • 不会长胖的斜杠 新星创作者: 后端开发技术领域 2022-04-29 13:29
    关注
    import random
    right = 0
    num = 1
    W = ['play', 'guess', 'english']
    repeat = 'yes'
    while num < len(W)+ 1:
        word = random.choice(W)
        word1 =word
        answer  = word
        a = ''
        for i in word:
            postion = random.randrange(len(word))
            a += word[postion]
            word = word[0:postion] + word[(postion+1):]
        print("乱序后的单词:", a)
        guess = input("输入你的猜测:")
        while guess != answer:
            guess = input("重新猜测:")
        print("猜对了")
        W.remove(word1)
        right += 1
    
    print("一共猜对了{}个词".format(right))
    print("游戏结束")
    
    

    望采纳!谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 5月7日
  • 已采纳回答 4月29日
  • 创建了问题 4月29日