求大佬幫助!!!
import os
def clear():os.system('cls')
import random
tre=[]
while True :
r=int(input("在此鍵入候選的人數:"))
s=r+1
for t in range(1,s) :
print("第",t,"位人是:")
n=input('在此鍵入:')
tre.append(n)
print("目前有:",str(tre))
tew=input("您確定要開始了嗎?(確定/增加/洗掉):")
while tew=='洗掉' :
shan=input("請輸入您要洗掉的一項(輸入\"q\"以取消):")
if shan=='q' :
break
elif shan in tre :
del tre[shan]
print("目前有:",str(tre))
t=t-1
else :
print("不存在的項!!!")
ji=input("您還要繼續洗掉嗎(繼續/終止):")
if ji=='終止' :
break
elif ji=='繼續' :
print("繼續洗掉!")
else :
print("錯誤的選項!!!")
break
while tew=='增加' :
t=t+1
print("第",t,"位人是:")
zeng=input('在此鍵入(輸入\"q\"以取消):')
if zeng=='q' :
t=t-1
break
else :
tre.append(zeng)
ji=input("您還要繼續增加嗎(繼續/終止):")
if ji=='終止' :
break
elif ji=='繼續' :
print("繼續增加!")
else :
print("錯誤的選項!!!")
break
print("目前有:",str(tre))
print("隨機篩選中……")
feas=int(random.randint(0,r))
string=tre[feas]
import time
tim=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
print("{}\000\000{}\000——被選中了!\n".format(tim,string))
with open('xuanzhong.txt','a') as file :
file.write("{}\00\00\00{}\000——被選中了!".format(tim,string))
we=input("您是否想要退出或者關機(是/否/關機):")
if we=='是' :
exit()
elif we=='否' :
clear()
tre=[]
elif we=='關機' :
os.system('shutdown -s -t 0')
else :
print("錯誤的選項!!!")
time.sleep(1.5)
exit()
在運行到del tre[shan]的時候
python報告:
Traceback (most recent call last):
File "C:\Users\惠普\Desktop\ttttt\老虎機.py", line 19, in <module>
del tre[shan]
TypeError: list indices must be integers or slices, not str
跪求大佬幫助
在線等!!!!
uj5u.com熱心網友回復:
用tre.remove[shan] del是洗掉串列中第幾個位置的元素uj5u.com熱心網友回復:
謝謝

uj5u.com熱心網友回復:
但是python又報告
Traceback (most recent call last):
File "C:\Users\惠普\Desktop\ttttt\老虎機.py", line 19, in <module>
tre.remove[shan]
TypeError: 'builtin_function_or_method' object is not subscriptable
我是萌新,不太懂這個
求幫助
uj5u.com熱心網友回復:
用() 括號不用[]uj5u.com熱心網友回復:
謝謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/251121.html
