我被要求写,模拟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