主要是,我试图让用户输入一些数据
def getmonsterData():
monster1 = raw_input("Enter the Monster Affinity|HP|AD> ")
x = monster1.index('|');
affinityType = monster1[0:x]
printNow(affinitytype)
我能够创建一个变量来存储怪物亲和性,因此在打印时它会列出用户输入的内容(即:火/土),但我不确定如何为 HP 和 AD 获取和创建类似的变量
这将打印我列出的 Monster1 的亲和性,例如火。这一切都很好, 但是当我不太确定如何使用 Jython/Python 拼接字符串时..可能像
HP = monster1[affinityType+1:x] ? not too sure how to do this
类似地,我需要获取最后一个变量 AD,我相信如果我获取 HP 变量我可以做到这一点
y = len(monster1)
AD = monster1[x+1:y]
对此的任何输入都会有所帮助 :) 谢谢
最佳答案
你可以使用拆分:
>>> aff,HP,AD = raw_input("Enter the Monster Affinity|HP|AD> ").split('|')
Enter the Monster Affinity|HP|AD> a|11|22
>>> aff
'a'
>>> HP
'11'
>>> AD
'22'
但是如果你想使用索引,你可以使用start参数(S.index(sub [,start [,end]]) -> int
)
>>> monster = raw_input("Enter the Monster Affinity|HP|AD> ")
Enter the Monster Affinity|HP|AD> a|11|22
>>> x = monster.index('|')
>>> monster[:x]
'a'
>>> y = monster.index('|',x+1)
>>> monster[x+1:y]
'11'
>>> monster[y+1:]
'22'
关于python - 我如何抓取输入字符串的某些部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27043031/