Home > OS >  How to use input() than choose list
How to use input() than choose list

Time:09-01

I write a logic to use input() varabile controling the list. The result show the same word for me,but I can't let the code work which I want. And find that 'c of the list 'is not equal 'c I input'.

name==name1 >>>>False

Could anyone tell me the different and how to solove? plz and txs

enter code here

#list

c=["A", "B", "C", "D"]

b=["A", "B", "C", "D"]

l=["A", "B", "C", "D"]

print('chest=c','back=b','leg=l')

name = input("please enter your training site(c、b、l) : ")

#random take list of 2(get and back)

name1=c

type(name1)

x = random.choices(name, k=6)

enter image description here

CodePudding user response:

First, I'd like to point out a couple of things:

  1. The return value of input() is str, so when you type a "c" on standard input, name == 'c'.

  2. When you write name1 = c, since you defined c = ["A", "B", "C", "D"], name1 == ["A", "B", "C", "D"], which is a list. list != str, which is why name == name1 is returning False.

Now, since it sounds like your goal is to present a different list based on the input, and each list corresponds to a particular body part, one way you could do this is by setting up a dict that maps a character to a list. Then, when the user enters a character, you don't have to worry about having to figure out how to assign lists; they're already assigned. An example of this is below:

site_map = {
    "c" : ["A", "B", "C", "D"],
    "b" : ["E", "F", "G", "H"],
    "l" : ["I", "J", "K", "L"]
    }
"""
Given the above dict, we have the following:
site_map['c'] will yield ["A", "B", "C", "D"]
site_map['b'] will yield ["E", "F", "G", "H"]
site_map['l'] will yield ["I", "J", "K", "L"]
"""

print("Chest = c, Back = b, Leg = l")

site = input("Please enter your training site (c, b, l): ")

# Error checking in case the user enters an invalid site
if ( not site in site_map.keys() ):
    print("Error: invalid site. Choose one of (c, b, l)")
    # quit program, you can use sys.exit(1) for this

result = site_map[site]

x = random.choices(result, k = 6)

print("Your list: %s" % str(x) )

If I've misunderstood your intentions or you have questions, don't hesitate to leave a comment.

  • Related