我被要求寫,模擬MU遊戲的程序:https://en.wikipedia.org/wiki/MU_puzzle在python追加字符字符串
假設有符號M,I和U可以組合產生的串符號。該MU拼圖問一個開始與「公理」串MI和使用中的每個步驟的下列轉換規則之一將其轉換成字符串MU:
Nr. Formal rule Informal explanation Example
--- ----------- -------------------------------------------- --------------
1. xI → xIU Add a U to the end of any string ending in I MI to MIU
2. Mx → Mxx Double the string after the M MIU to MIUIU
3. xIIIy → xUy Replace any III with a U MUIIIU to MUUU
4. xUUy → xy Remove any UU MUUU to MU
這是我到目前爲止有:
string = input("Enter a combination of M, U and I: ")
while True:
if string and all(c in "MIU" for c in string):
print("This is correct")
break
continue
else:
print("This is incorrect")
break
rule = input("Enter a rule 1-4 or q to quit: ")
rule1 = (string+'U')
while True:
if rule == ('1') and string.endswith('I'):
print(rule1)
break
continue
elif rule == ('2') and string.startswith('M'):
但是,我卡在第二條規則。我假設它問我從字符串的範圍中的起始點1打印字符串,因爲M將是0點,然後將它們加在一起形成一個新的字符串?我不完全確定如何做到這一點。
請修正您的代碼的縮進 – skrrgwasme
最新的錯誤是什麼?它在我執行時有效 – discord1
有點像's ='M'+ s [1:] * 2'? – soon