python - 在设计猜猜 python 中的数字列表的游戏时遇到麻烦

标签 python python-2.x

今天我想设计一个游戏来猜测我设置的数字列表。玩家应该打印出所有 5 个数字才能赢得游戏。并且不允许打印重复的数字。代码如下:

def guess(): 

   print "Please input a number smaller than 10, let's see if it is in my plan."

   print "You should figure out all the 5 numbers."

   n = int(raw_input('>'))
   my_list = [1, 3, 5, 7, 9]
    your_list = []
    count = 0
    while count < 5:
        if n in my_list:
            if n not in your_list:
                your_list.append(n) 
                count = count + 1
                print "Good job! You have got %d numbers!" % count
                n = int(raw_input('>'))
            else:
                print "You have already typed that. Input again!"
                n = int(raw_input('>'))
        else:
            print "That is not what I want."
            n = int(raw_input('>'))
    print "Here you can see my plan:", my_list
    print "You are so smart to  guess out, you win!"

guess()

当我尝试运行它时,看到结果我很困惑:

 Please input a number smaller than 10, let's see if it is in my plan
 You should figure out all the 5 numbers
>1
Good job! You have got 1 numbers
>2
That is not what I want
>3
Good job! You have got 2 numbers
>5
Good job! You have got 3 numbers
>7
Good job! You have got 4 numbers
>4
Here you can see my plan: [1, 3, 5, 7, 9]
You are so smart to  guess out, you win!

当我键入 4 时,它应该打印“这不是我想要的”而不是指示“赢”。为什么会出错?

最佳答案

在我这边,它正在工作。唯一一点不起作用,我需要在退出 while 循环之前输入第 6 个元素。我将向您简要介绍可能的改进和不同的实现方式。

此外,您正在使用 python 2。您应该考虑转向 python 3,尤其是如果您刚刚开始学习它的话。

改进:

  • while count < 5:可以简单地变成while sorted(my_list) != sorted(your_list): .
  • 可以简化嵌套语句。
  • 检查输入就好了。

实现:

def guess_2():
    print ("Please input a number smaller than 10, let's see if it is in my plan.")
    print ("You should figure out all the 5 numbers.")

    # Parameters
    my_list = [1, 3, 5, 7, 9]
    your_list = []
    count = 0

    while sorted(my_list) != (sorted(your_list)):

        # Input:
        not_valid = True
        while not_valid:
            try:
                n = int(input('>'))
                not_valid = False
            except:
                print ("Please input a number.")

        if n in my_list and not n in your_list:
            your_list.append(n) 
            count = count + 1
            print ("Good job! You have got %d numbers!" % count)

        elif n in my_list and n in your_list:
            print ("You have already typed that. Input again!")

        else:
            print ("That is not what I want.")

    print ("Here you can see my plan:", my_list)
    print ("You are so smart to  guess out, you win!")

关于python - 在设计猜猜 python 中的数字列表的游戏时遇到麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51495728/

相关文章:

python - 写入 .txt 文件(UTF-8),python

python - 为什么 sys.stdout.encoding 在管道输出时会有所不同(在 Python2.x 中)?

Python 2, map 在简单情况下不等同于列表理解;长度依赖

Python:无法将字符串转换为 int

python - 如何让我的电子邮件每封电子邮件只显示一次?

python - Flask 和其他 Python 框架中的多个 URL 段

python - Flask-SocketIO 不能在 Apache/WSGI 上工作

python - 队列.close() "raises"BrokenPipeError

Python h5py swmr模式: Can't read data while writer has the file open

python - 在字典列表中编码