【问题标题】:While loop doesn't work/loops forever | Beginner question [duplicate]虽然循环不起作用/永远循环 |初学者问题[重复]
【发布时间】:2022-01-12 14:30:39
【问题描述】:

以下是我对这个问题的尝试解决方案:通过将 1 到 30 之间的三个数字与以相同顺序抽取的三个随机数字相匹配来赢得彩票中的头奖。当一个球被抽出时,它会在另一个球被抽出之前放回机器中。在抽出一个球之前,机器中总是有 30 个球,玩家可以多次选择同一个球。每周进行一次抽奖。编写一个以三个数字为参数的函数,在 1 到 30 之间抽取三个随机数,并返回赢得大奖所需的周数。 (例如,选择的数字:17、12、25 必须匹配:第 1 球是 17,第 2 球是 12,第 3 球是 25。)

我的尝试:

import random

chosen = []
for i in range(0, 3, 1):
  chosen.append(input("Please input your lucky number: "))

drawn_numbers = []
count = 0
week_count = 0

print("Your chosen numbers are: {}, {}, {}".format(chosen[0], chosen[1], chosen[2]))

while count != 3:
  for i in range(0, 3, 1):
    random_number = random.randint(1, 30)
    drawn_numbers.append(random_number)
  
  for i in range(0, 3, 1):
    if drawn_numbers[i] in chosen:
      count += 1
    
  week_count += 1
  drawn_numbers = []

print("It took you {} weeks to win.".format(week_count))

由于某种原因,while 循环只是忽略了count += 1 部分并永远循环随机生成的drawed_numbers 列表。

显然我的循环有问题,但我看不到它 D:

关于如何使该循环工作的一些建议会很好。谢谢。

【问题讨论】:

  • chosen 中的值是字符串,不是整数。

标签: python loops random while-loop


【解决方案1】:

首先 - 欢迎来到 Python 世界!

你有几个错误:

  • input 函数返回 string 而不是 int。意思是,您尝试在 if drawn_numbers[i] in chosen 中隐式进行的比较永远不会为真,因此 count 始终为 0,您将陷入无限循环。

  • 假设您已修复上述错误,您仍有机会count 在循环评估时永远不会达到 3。考虑以下场景:

    1. 用户输入数字[1, 2, 3]
    2. drawn_numbers[1, 2, 4]
    3. 因此,count 现在将变为 2
    4. while 条件是 count != 3True,因此循环继续。
    5. 再次抽出的数字是[1, 2, 4]
    6. count 现在将是 4
    7. 从现在开始,count 只能增加,永远不会等于3

    你知道这是怎么回事吗?尝试在循环开始(内部)重置count

  • 通过匹配三个数字...以相同的顺序

    中奖

    但您实现的比较仅在列表中查找存在,而不是逐个元素进行比较。


因此,一个有效的(不是以最 Pythonic 方式编写的)版本将是:

import random

chosen = []
for i in range(0, 3, 1):
    chosen.append(int(input("Please input your lucky number: ")))  # notice the int cast

drawn_numbers = []
count = 0
week_count = 0

print("Your chosen numbers are: {}, {}, {}".format(chosen[0], chosen[1], chosen[2]))

while count != 3:
    count = 0  # notice that count is reset here 
    
    for i in range(0, 3, 1):
        random_number = random.randint(1, 30)
        drawn_numbers.append(random_number)
    
    for i in range(0, 3, 1):
        if drawn_numbers[i] == chosen[i]:  # notice that we compare the elements in the same location
            count += 1

    week_count += 1
    drawn_numbers = []

print("It took you {} weeks to win.".format(week_count))


如果你有兴趣,这里有一个更 Pythonic 的方式:

import random

chosen = [int(input("Please input your lucky number: ")) for _ in range(3)]
print(f'Your chosen numbers are: {chosen}')

matched = False
week_count = 0

while not matched:
    drawn_numbers = [random.randint(1, 30) for _ in range(3)]    
    matched = drawn_numbers == chosen
    week_count += 1

print(f"It took you {week_count} weeks to win.")

您可能会在其中发现一些您可能不熟悉的功能,但我强烈建议您掌握它们:

  1. List comprehension
  2. range(3) 等价于 range(0, 3, 1)
  3. Formatting string literals
  4. == 运算符实际上逐个元素地比较两个列表!

有趣的旁注:在没有订单限制的情况下运行此代码会导致等待中奖的时间要少得多:)

【讨论】:

    【解决方案2】:

    您是否检查过您的 if 语句是否为真?

    一个想法是,选择的可能包括字符串值作为数字。尝试运行:

    print(type(chosen[0]))
    

    这应该是一个整数。修复它:

    chosen.append(int(input("Please input your lucky number: ")))
    

    【讨论】:

      猜你喜欢
      • 2015-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-24
      相关资源
      最近更新 更多