Python 3.x 中的 RNG Name Picker 被操纵

Rigged RNG Name Picker in Python 3.x

我有一个朋友要搬进新公寓,他问我是否可以为公寓构建一个 'rigged' 按需名称选择器。到目前为止,我已经构建了这个,但是我遇到了圈复杂度的错误(我可以理解,连续有 30 个 if/else 循环)。我只是想知道将随机数分配给整数、比较它们然后将它们按升序列出到字符串的正确方法是什么。

程序应该给任何输入大写 'R' 作为名字的人一个较低的分数,因此打印出他们最后选择的房间,一旦通过大约 30 if/else 语句进行比较以确定哪个数字是 higher/lower(例如见下文)。大写的 'R' 位是为了让你可以输入小写的每个人的名字来证明程序的真正随机性,然后在真实的 运行 thru 上将每个人的名字大写(我敢肯定有偷偷摸摸的技术但是这是我能想到的全部)。

我从第一个 is/else 语句中得到圈复杂度标志,所以我认为问题可能出自这里

    `print("Hello")
from random import seed
from random import randint
import time

flatmate1 = "zero"
flatmate2 = "zero"
flatmate3 = "zero"
flatmate4 = "zero"

first = 0
second = 0
third = 0
fourth = 0
value1 = 0 
value2 = 0
value3 = 0
value4 = 0
name1 = 0
name2 = 0
name3 = 0
name4 = 0

flatmate1 = input("Enter flatmate 1 name:")
flatmate2 = input("Enter flatmate 2 name:")
flatmate3 = input("Enter flatmate 3 name:")
flatmate4 = input("Enter flatmate 4 name:")

print("Generating random numbers")
time.sleep(2)


if flatmate1[0] == ("R"): 
    value1 = randint(1, 20)
    name1 = value1
else: name1 = randint(21, 100) 

if flatmate2[0] == ("R"): 
    value2 = randint(1, 20)
    name2 = value2
else: name2 = randint(21, 100) 

if flatmate3[0] == ("R"): 
    value3 = randint(1, 20)
    name3 = value3
else: name3 = randint(21, 100) 

if flatmate4[0] == ("R"): 
    value4 = randint(1, 20)
    name4 = value4
else: name4 = randint(21, 100) 

print(name1, name2, name3, name4)
print(flatmate1, flatmate2, flatmate3, flatmate4)


if (name1 > name2 and name3 and name4):
    print (flatmate1 + " chooses first")
    first = 1

elif (name2 > name3 and name4 and name1):
    print (flatmate2 + " chooses first")
    first = 2

elif (name3 > name4 and name1 and name2):
    print (flatmate3 + " chooses first")
    first = 3

elif (name4 > name1 and name2 and name3):
    print (flatmate4 + " chooses first")
    first = 4

if first == 1:
    if (name2 > name3 and name4):
        print (flatmate2 + " chooses second")
        second = 2

    elif (name3 > name4 and name2):
        print (flatmate3 + " chooses second")
        second = 3

    elif (name4 > name2 and name3):
        print (flatmate4 + " chooses second")
        second = 4    

elif first == 2:
    if (name1 > name3 and name4):
        print (flatmate1 + " chooses second")
        second = 1

    elif (name3 > name4 and name1):
        print (flatmate3 + " chooses second")
        second = 3

    elif (name4 > name1 and name3):
        print (flatmate4 + " chooses second")
        second = 4  

elif first == 3:
    if (name1 > name2 and name4):
        print (flatmate1 + " chooses second")
        second = 1

    elif (name2 > name4 and name1):
        print (flatmate2 + " chooses second")
        second = 2

    elif (name4 > name1 and name2):
        print (flatmate4 + " chooses second")
        second = 4

elif first == 4:
    if (name1 > name3 and name2):
        print (flatmate1 + " chooses second")
        second = 1

    elif (name3 > name2 and name1):
        print (flatmate3 + " chooses second")
        second = 3

    elif (name2 > name1 and name3):
        print (flatmate2 + " chooses second")
        second = 2

if second == 1 and first == 2:
    if (name4 > name2):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name3 > name4):
        print (flatmate3 + " chooses third")
        third = 3

elif second == 2 and first == 1:
    if (name4 > name3):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name2 > name4):
        print (flatmate2 + " chooses third")
        third = 2    

elif second == 1 and first == 3:
    if (name4 > name2):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name2 > name4):
        print (flatmate2 + " chooses third")
        third = 2

elif second == 3 and first == 1:
    if (name4 > name2):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name2 > name4):
        print (flatmate2 + " chooses third")
        third = 2

elif second == 1 and first == 4:

    if (name2 > name3):
        print (flatmate2 + " chooses third")
        third = 2

    elif (name3 > name2):
        print (flatmate3 + " chooses third")
        third = 3

elif second == 4 and first ==1:

    if (name2 > name3):
        print (flatmate2 + " chooses third")
        third = 2

    elif (name3 > name2):
        print (flatmate3 + " chooses third")
        third = 3

elif second == 2 and first == 3:
    if (name4 > name1):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name1 > name4):
        print (flatmate1 + " chooses third")
        third = 1

elif second == 3 and first == 2:
    if (name4 > name1):
        print (flatmate4 + " chooses third")
        third = 4

    elif (name1 > name4):
        print (flatmate1 + " chooses third")
        third = 1

elif second == 2 and first == 4:
    if (name3 > name1):
        print (flatmate3 + " chooses third")
        third = 3
    elif (name1 > name3):
        print (flatmate1 + " chooses third")
        third = 1

elif second == 4 and first == 2:
    if (name3 > name1):
        print (flatmate3 + " chooses third")
        third = 3
    elif (name1 > name3):
        print (flatmate1 + " chooses third")
        third = 1

elif second == 3 and first == 4:
    if (name2 > name1):
        print (flatmate2 + " chooses third")
        third = 2

    elif (name1 > name2):
        print (flatmate1 + " chooses third")
        third = 1

elif second == 4 and first == 3:
    if (name2 > name1):
        print (flatmate2 + " chooses third")
        third = 2

    elif (name1 > name2):
        print (flatmate1 + " chooses third")
        third = 1

if (name1 < name2 and name3 and name4):
    print(flatmate1 + " chooses last")
elif (name2 < name1 and name3 and name4):
    print(flatmate2 + " chooses last")
elif (name3 < name2 and name1 and name4):
    print(flatmate3 + " chooses last")
elif (name4 < name2 and name3 and name1):
    print(flatmate4 + " chooses last")

numbers = [name1, name2, name3, name4] 
numbers.sort(reverse=True) 
print(numbers)`

如有任何帮助,我们将不胜感激

*已编辑

您好,欢迎来到 stack overflow。这看起来像一个有趣的小项目。但是,是的,你把它变得太复杂了!让我们看看如何清理它。

TLDR,你真正的错误在于比较。这个:

if (name1 < name2 and name3 and name4):

不会给你你的意思。它将检查第一个条件,然后检查 name3 和 name4 是否有值。你的意思可能是这样的:

if (name1 < name2 and name1 < name3 and name1 < name4):

但是有更好的方法来对名称列表进行排序!我已经把它清理干净并在下面给你看。

事先做一些说明: 1)我知道你不知道问题出在哪里。但是,在您提出问题之前,您应该做一些日志记录以尝试隔离问题,以便您可以 post 一个简单的、可重现的示例。 2) 请不要 post 链接到代码。 Post 代码。如果您真的可以编辑您的问题来解决这个问题,那将是最好的。这也是您需要隔离问题的原因。 3) 考虑使用代码审查堆栈交换。

好吧,你开始吧:

# Tip 1: Be careful with uninformative print messages! They will just clutter your output.
print("This is a sanity-check message. If this displays, then the program is running.")

from random import seed
from random import randint
import time

# Tip 2: in Python, you don't need to initialise variables! I've deleated all that.

# Tip 3: DRY (Don't repeat yourself!). This is the same logic four times, so let's use a list and a loop!

# This list will store all of the flatmate names
flatmates = []

# This list will store the random numbers assigned to each flatmate
flatmate_scores = []

# I'm declaring this separately (and centrally), so that you can change it easily. Always write code in a way that is 
# easiest to change (also known by the cool kids as 'agile' development).
NUMBER_OF_FLATMATES = 4

for i in range(NUMBER_OF_FLATMATES):
    flatmates.append(input("Enter the name of flatemate number " + str(i)))

print("Generating random numbers...")
time.sleep(2)


for flatmate in flatmates:
    if flatmate[0] == "R":
        # Why create a 'value' variable if you are immediately going to 
        # assign it to a 'name' variable?
        # also, 'name' was a terrible variable name for this anyway!
        score = randint(1, 20)
    else:
        score = randint(21, 100)

    flatmate_scores.append(score)


for i in range(NUMBER_OF_FLATMATES):
    print(flatmates[i], end=', score: ')
    print(flatmate_scores[i])


# Here is where you make your first actual mistake. 
# You cannot say 'if (name1 > name2 and name3)'
# You would have to say 'if (name1 > name2 and name1 > name3)'
# etc. But there is a better way.
# We will combine the names and scores into a tuple,
# (When you are more experienced, you can make a class and objects.
# but for now a tuple is an easy data structure to use). We'll 
# make the tuple using Python's zip function 

names_with_scores = zip(flatmates, flatmate_scores)

# Now we can just sort this list using a key which tells Python to sort by 
# The second element (x[1]), which is the flatmate score:

names_with_scores = sorted(names_with_scores, key=lambda x:x[1])

# Reverse so high score means good.
names_with_scores = reversed(names_with_scores)
print("Flatmates should choose in the following order:")
for name, _ in names_with_scores:
    print(name)

# And boom. If you ignore comments, this is about 5X shorter than your original script,
# and much easier to understand.