【问题标题】:A while loop for specific integers (Python)特定整数的while循环(Python)
【发布时间】:2021-11-24 10:11:47
【问题描述】:

所以我使用 Python 中的 randint() 函数随机生成了 2 个数字。假设这些数字是 x1 和 x2。游戏玩家必须猜测这些数字的除法值,并给出一个偏差,这意味着猜测与正确答案相差多少百分比。数字变得越来越困难。

现在我有一个 while 循环(正确答案 = x1 // x2):

print(int(input("Guess: ")))
  while guess != correct_answer or guess == correct_answer:
    print(f"{deviation}, next numbers: {x1}, {x2}") 
    print(int(input("Insert your guess: ")))
    print(f"{deviation}, next numbers: {x1}, {x2}")  #i want to change the difficulty here
    break

我可以简单地写 x1 + number 来改变 x1 的值并使其更困难,但偏差仍然与第一个难度使用的偏差相同。我不知道如何通过尝试的不同难度来改变偏差。 偏差百分比写为(guess - correct_answer)*100

【问题讨论】:

  • 百分比不是这样计算的。另外,我真的不明白while guess != correct_answer or guess == correct_answer: 涵盖了两种可能的情况
  • 这就是重点——我想要一个循环,这两种情况都是可能的,这样用户就可以继续第二个困难。我使用这种计算百分比的方法,以便用户知道他/她与正确答案相差多少。因此,如果用户猜测 2 而不是 1,那么他/她与正确答案的差距为 (2-1)*100 = 100%。
  • 这仍然不是百分比的计算方式;您需要对此进行一些研究,因为它是非常核心的东西。而且,该检查的逻辑仍然没有意义-您不妨使用while True:
  • 哦,是的,等等,我的意思是 ((2-1):(1))X100,不知道为什么我写错了公式。仍然不知道“该检查的逻辑没有意义”是什么意思。那有什么意义呢?我需要一个while循环。
  • @AnonymousRU 是的,但似乎它也可能只是 while True: 并且在循环中您将有 if/elif 语句来检查是否将 break 退出循环或继续下一个难度,这也不是minimal reproducible example(即,如果我要复制粘贴您的代码,它将不起作用)

标签: python math input integer


【解决方案1】:

据我了解,这就是您要创建的内容:

import random

for i in range(5):
    x1, x2 = sorted(random.choices(range(1, 10 ** (i + 1)), k=2), reverse=True)
    print(f'Guess the result of FLOOR division of the following numbers:\n'
          f'{x1} and {x2} ({x1} divided by {x2})')
    guess = int(input('Your guess: '))
    result = x1 // x2
    if guess == result:
        print('You guessed correctly!\n')
        continue
    deviation = abs((guess - result) / result) * 100
    print(f'Your answer was {deviation: .0f}% away from the correct answer of {result}\n')

难度越来越大的楼分猜测者,也能说出与正确答案的偏差。

要简单地对抗ZeroDivisionError,您可以添加以下内容:

try:
    deviation = abs((guess - result) / result) * 100
except ZeroDivisionError:
    deviation = 0

但更简单的方法是通过将 x1 排序为大于 x2 来简单地不让 result 为 0,如果使用 random.randint 可以这样做:

x1, x2 = sorted([random.randint(1, 10 ** (i + 1)), random.randint(1, 10 ** (i + 1))], reverse=True)

完整示例:

import random

tries = 5
user_stats = list()

for i in range(5):
    abs_range = 10 ** (i + 1)
    x1 = random.randint(-abs_range, abs_range)
    x2 = random.choice(list(range(-abs_range, 0)) + list(range(1, abs_range + 1)))
    print(f'Guess the result of FLOOR division of the following numbers:\n'
          f'{x1} and {x2} ({x1} divided by {x2})')

    user_stats.append(list())

    for j in range(tries):
        guess = int(input('Your guess: '))
        result = x1 // x2
        if guess == result:
            print('You guessed correctly!\n')
            user_stats[-1].append('Success')
            break
        deviation = (guess - result) / result * 100 if result != 0 else 0
        print(f'Your answer was {deviation: .0f}% away from the correct answer\n')
        user_stats[-1].append((guess, deviation, j + 1))
    else:
        print('Game failed!')
        break

print(user_stats)

来源:

【讨论】:

  • 我试过你的代码,它工作正常!你是如何让偏差不断变化的?那是 {deviation: .0f} 部分吗?并且有没有更简单的方法来写下这个,因为这个类的级别比这更容易。
  • @Mattiss 顺便说一句,我使用了 random.randint() 模块
  • @AnonymousRU 偏差是通过公式简单计算的:deviation = abs((guess - result) / result) * 100,它只是根据结果和用户输入的内容而变化,{deviation: .0f} 只是在后面显示 0 位的格式. 所以基本上是一个整数值(否则它将是百分比.0%(删除 : .0f 部分,你会看到,将 0 更改为 2 或其他,然后你会看到)。
  • @AnonymousRU 确定你可以使用random.randint 函数(不是模块,但这是一个技术性问题),我只是使用了.sample,它接受一个序列(在这种情况下,范围(它改变通过循环))然后从该序列中获取两个随机项(第二个参数),因此它返回一个列表,然后对该列表进行反向排序(sorted 也返回一个列表),以便larger divided by smaller,否则答案将如果smaller divided by larger 很容易猜到,则为 0
  • @Mattiss 啊,我明白了。是的,因为我是 python 新手,所以我不知道所有的技术术语,抱歉哈哈。那么我怎么能用 random.randint 函数编写一个循环呢?我想每次 x1 加 5,x2 减 10。
猜你喜欢
  • 2021-11-28
  • 2012-12-12
  • 2014-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多