向用户询问优先级列表,然后根据其对列表进行排序

问题描述 投票:1回答:1

我正在研究一个小程序,其中有4个电灯开关。仅当3个开关中的3个开关打开时,指示灯才点亮。我想问用户是否应该设置优先级,如果需要,应该设置什么优先级。假设C开关必须先向上,然后是A,然后是B。如果用户按此顺序输入1-s(1-开关接通,0-开关断开),则灯将打开,否则它将点亮。保持关闭状态。我有点被困住了,不知道该怎么办,也许我在补偿问题。谢谢。

def sort(arr):
    n = len(arr)
    for i in range(n):
        for j in range(0, n - i - 1):
            if arr[j] > arr[j + 1]:
                arr[j], arr[j + 1] = arr[j + 1], arr[j]


A = B = C = D = int()
tomb = []

sorrend = input("What should be the switches order: ")
lista = sorrend.split()
print("Switches order: ")
print(lista)


while [A, B, C, D] != 0 or [A, B, C, D] != 1:
    A = int(input("A switch (1-on, 0-off): "))
    B = int(input("B switch  (1-on, 0-off): "))
    C = int(input("C switch  (1-on, 0-off): "))
    D = int(input("D switch  (1-on, 0-off): "))
    if 1 in (A, B, C, D) or 0 in (A, B, C, D):
        arr.append(A)
        arr.append(B)
        arr.append(C)
        arr.append(D)
        break
    else:
        print("The switch status could be 1 or 0.")

print(arr)

sort(arr)


print('sorted arr:')
for i in range(len(arr)):
    print(arr[i], end=" ")
python python-3.x
1个回答
0
投票
from random import randrange

def value_sum(dic):
    if sum(dic.values()) > 2:
        print("The lamp is on!")
    else:
        print("The lamp is off!")

dic={"A": None, "B": None, "C": None, "D": None}
question = input("Do you want to specify the order of switches and their state? (yes/no) ")

if question == "no":
    for i in dic.keys():
        dic[i] = randrange(2)
        print("{} state is {}".format(i, dic[i]))
    value_sum(dic)
if question == "yes":
    sorrend = input("What should be the switches(A B C D) order: ")
    lista = sorrend.split()
    print("Switches order is: ")
    print(lista)

    for i in lista:
        a = int(input("{} switch (1-on, 0-off): ".format(i)))
        if a in range(0,2):
            dic[i] = a
        else:
            print("The switch {} status should be 1-on or 0-off.".format(i))
    value_sum(dic)
else:
    exit()
© www.soinside.com 2019 - 2024. All rights reserved.