我撰寫了一個邏輯來使用 input() 變數來控制串列。結果對我來說顯示了相同的詞,但我不能讓代碼按我的意愿作業。并發現 'c of the list' 不等于 'c I input'。
名稱==名稱1 >>>>錯誤
誰能告訴我不同??的地方以及如何去愛?請和TXS
enter code here
#串列
c = [“A”,“B”,“C”,“D”]
b = [“A”,“B”,“C”,“D”]
l=[“A”、“B”、“C”、“D”]
列印('胸部=c','背部=b','腿=l')
name = input("請輸入您的培訓地點(c、b、l) : ")
#random take list of 2(get and back)
名稱1=c
型別(名稱1)
x = random.choices(名稱, k=6)
在此處輸入影像描述
uj5u.com熱心網友回復:
首先,我想指出幾點:
的回傳值為,因此當您在標準輸入
input()
中str
鍵入“c”時,name == 'c'
.當你寫的時候
name1 = c
,既然你定義了c = ["A", "B", "C", "D"]
,,name1 == ["A", "B", "C", "D"]
那就是一個list
。list != str
,這就是為什么name == name1
要回傳False
。
現在,由于聽起來您的目標是根據輸入呈現不同的串列,并且每個串列對應于特定的身體部位,因此您可以這樣做的一種方法是設定dict
將字符映射到串列的 a。然后,當用戶輸入一個字符時,您不必擔心必須弄清楚如何分配串列;他們已經被分配了。下面是一個例子:
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) )
如果我誤解了您的意圖或您有任何疑問,請隨時發表評論。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/506963.html
上一篇:如何并排列印鍵和值?