在打印列表中的值后,如何向列表中添加字符串说欧姆?

时间:2018-01-29 13:26:44

标签: python python-3.x visual-studio pycharm

  
    

用户切换案例的选项。如果你想运行使用输入0,1,3作为选项输入cuz所有的选项值现在还没有分配

  
option1=input("Choose the First Colour\n"
              "0. Black\n"
              "1. Brown\n"
              "2. Red \n")
option2= input("Choose the Second Colour\n"
               "0. Black\n"
               "1. Brown\n"
               "2. Red\n")

option3=input(" Choose the Third color\n "
              "0. Black\n"
              "2. Brown\n"
              "3. Red\n")
Black, blue, red = 5, 4,7

    if option1 =='0':
        print(option2)
        a=[]
        a.append(Black)
    elif option1=='1':
        print(option2)
    else:
        print(option2)

if option2=='0':
    print(option3)
elif option2=='1':
    print(option3)
    a.append(blue)
else:
    print(option3)


if option3=='3':
    a.append(red)
    for item in range (0, len(a)):
        print(a[item], end="")
  
    

该程序提供的输出是

  
547
  
    

我需要的输出是

  
547 ohms
  
    

请帮我在打印值后在列表中添加名为“ohms”的字符串     注意:如果你正在调试程序,请记住现在只使用输入0,1,3。

  

2 个答案:

答案 0 :(得分:0)

为什么不在for循环后添加print ohms。 像这样:

if (option3=='3'):
 a.append(red)
 for item in range (0, len(a)):
    print(a[item],end="")
 print(" ohms")

for循环可以更好地编写,如:

if (option3=='3'):
 a.append(red)
 for item in a:
    print(item,end="")
 print(" ohms")

或者,在列表中添加“ohms”项, 打印列表:

if (option3=='3'):
    a.append(red)
    a.append(" ohms")
    for item in a:
        print(item,end="")

更新:问题海报添加了自己的错误代码作为答案,并要求我完成它!代码遍布整个地方,使问题更加混乱。我建议他以明确的方式重新表述这个问题。 我想他想对某些电路问题进行某种计算,其中电阻值根据3个位置和所选择的颜色而变化。这是基于他的“答案”中详细代码的更新。

OptionS12 = {"0":1, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, \
             "7":7, "8":8, "9":9}  # option1 and 2 according to the menu items above
OptionS3 = {"0":1, "1":10, "2":200, "3":3000, "4":40000, "5":500000, "6":6000000, \
            "7":70000000, "8":800000000, "9":9000000000}  # option 3 according to the menu items above
a = [] # my calculated outputs will be in this list
for option in [option1,option2]: # for each option
 if (option in OptionS12):
    print(option)
    a.append(OptionS12[option])  # add the value for that option from the color_codes dictionary of values
 else:
     print("option does not exist",option)
if option3 in OptionS3:
    print(option3)
    a.append(OptionS3[option3])  # add the value for that option from the color_codes dictionary of values
    if option3 in ["0","1","2"]:
        a.append(" ohms")
    elif option3=="3":
        a.append(" kilo ohms")
    else:
        a.append(" mega ohms")
else:
    print("option does not exist", option3)
for item in a:
    print(item,end="")

答案 1 :(得分:0)

    option1 = input("Choose the First Colour\n"
                "0. Black\n"
                "1. Brown\n"
                "2. Red \n"
                "3. Orange\n"
                "4. Yellow\n"
                "5. Green\n"
                "6. Blue\n"
                "7. Violet\n"
                "8. Grey\n"
                "9. White\n")

option2 = input("Choose the First Colour\n"
                "0. Black\n"
                "1. Brown\n"
                "2. Red \n"
                "3. Orange\n"
                "4. Yellow\n"
                "5. Green\n"
                "6. Blue\n"
                "7. Violet\n"
                "8. Grey\n"
                "9. White\n")

option3 = input("Choose the First Colour\n"
                "0. Black\n"
                "1. Brown\n"
                "2. Red \n"
                "3. Orange\n"
                "4. Yellow\n"
                "5. Green\n"
                "6. Blue\n"
                "7. Violet\n"
                "8. Grey\n"
                "9. White\n")

Black, Brown, Red, Orange, Yellow = 0, 1, 2, 3, 4
Green, Blue, Violet, Grey, White = 5, 6, 7, 8, 9
################################################
if option1 == '0':
    print(option2)
    a = []
    a.append(Black)
elif option1 == '1 ':
    print(option2)
    b = []
    b.append(Brown)
elif option1 == '2':
    print(option2)
    c = []
    c.append(Red)
elif option1 == '3':
    print(option2)
    d = []
    d.append(Orange)
elif option1 == '4':
    print(option2)
    e = []
    e.append(Yellow)
elif option1 == '5':
    print(option2)
    f = []
    f.append(Green)
elif option1 == '6':
    print(option2)
    g = []
    g.append(Blue)
elif option1 == '7':
    print(option2)
    h = []
    h.append(Violet)
elif option1 == '8':
    print(option2)
    i = []
    i.append(Grey)
elif option1 == '9':
    print(option2)
    j = []
    j.append(White)


####################################

if option2 =='0':
    print(option3)
    a.append(Black)
elif option2 == '1 ':
    print(option3)
    b.append(Brown)
elif option2 == '2':
    print(option3)
    c.append(Red)
elif option2 == '3':
    print(option3)
    d.append(Orange)
elif option2 == '4':
    print(option3)
    e.append(Yellow)
elif option2 == '5':
    print(option3)
    f.append(Green)
elif option2 == '6':
    print(option3)
    g.append(Blue)
elif option2 == '7':
    print(option3)
    h.append(Violet)
elif option2 == '8':
    print(option3)
    i.append(Grey)
elif option2 == '9':
    print(option3)
    j.append(White)

#############################

if option3=='0':
    a.append('')
    a.append(' ohms')
    for iteml in range (0, len(a)):
        print(a[iteml], end="")
elif option3=='1':
    b.append('0')
    b.append(' ohms')
    for item in range (0, len(b)):
        print(b[item], end="")
elif option3=='2':
    c.append('00')
    c.append(' ohms')
    for item in range (0, len(c)):
        print(c[item], end="")
elif option3 =='3':
    d.append('000')
    d.append(' Kilo ohms')
    for item in range (0, len(d)):
        print(d[item], end="")
elif option3=='4':
    e.append('0000')
    e.append(' Kilo ohms')
    for item in range (0, len(e)):
        print(e[item], end="")
elif option3=='5':
    f.append('00000')
    f.append(' Mega ohms')
    for item in range (0, len(f)):
        print(f[item], end="")
elif option3=='6':
    g.append('000000')
    g.append(' Mega ohms')
    for item in range (0, len(g)):
        print(g[item], end="")
elif option3=='7':
    h.append('0000000')
    h.append(' Mega ohms')
    for item in range (0, len(h)):
        print(h[item], end="")
elif option3=='8':
    i.append('00000000')
    i.append(' Mega ohms')
    for item in range (0, len(i)):
        print(i[item], end="")
if option3=='9':
    j.append('000000000')
    j.append(' Mega ohms')
    for item in range (0, len(j)):
        print(j[item], end="")
  
    

上面这段代码中的问题是我无法计算不同的电阻颜色代码。说我想计算 RED YELLOW BLUE,它给出了错误..所有都没有附加在同一个列表中;而如果我从选项输入计算相同的颜色代码 RED RED RED ,则可以计算出来。事实上,在追加行中存在错误,因为所有值都附加在一个列表中但是为了计算不同的颜色代码,它需要将值传递给来自不同选项的其他列表。你可以运行和修改程序来计算电阻器的不同颜色代码。