我需要在程序中創建'while'循環,但無法正確完成。下面是我到目前爲止有:如何在Python中爲兩個答案寫'不等於'?
restart ="y"
while restart == "y":
sentence = input("What is your sentence?: ")
sentence_split = sentence.split()
sentence2 = [0]
print(sentence)
for count, i in enumerate(sentence_split):
if sentence_split.count(i) < 2:
sentence2.append(max(sentence2) + 1)
else:
sentence2.append(sentence_split.index(i) +1)
sentence2.remove(0)
print(sentence2)
outfile = open("testfile.py", "wt")
outfile.write(sentence)
outfile.close()
print (outfile)
restart = input("would you like restart the programme y/n?")
if restart == "n":
print ("programme terminated")
else:
print("you were asked y or n")
當進入「N」是那麼迴路工作正常,它打印「節目結束」,但「Y」被輸入時,它會重新啓動該程序,但也還是打印「你被問y或n「。
不,這不是。他有一段時間來重新啓動他的程序。他不只是想打印''重新啓動''。 –
@leaf好吧,這不會打印出「你被問及或你」,這是我認爲他想要的,他究竟想要什麼? –
也許我誤解了你。這是否意味着工作代碼?如果不是,我表示歉意,可能會意外地得出一些錯誤的結論。 –