我的代码现在是工作的,但只有当我输入1值,但如果我输入2个值,它将无法工作。示例(我将输入monolithic_suppressor;owc_skeleton_stock)编译器不会读取它。我想要输入多个附件,这些附件添加或减去下面的man统计数据属性。
damage = 49
fire_rate = 50
accuracy = 69
mobility = 59
shooting_range = 56
controls = 53
attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
attachment = str(input("What attachment would you like to add? ")).lower()
while True:
if attachment not in attachments:
break
elif attachment == attachments[0]:
damage += 5
mobility -= 5
elif attachment == attachments[1]:
mobility += 5
accuracy -= 8
elif attachment == attachments[2]:
accuracy += 5
controls += 5
elif attachment == attachments[3]:
controls += 10
mobility += 2
print(f"""\n Man O War Updated Stats!\n Damage: {damage}\n Fire Rate: {fire_rate}\n Accuracy: {accuracy} \n Mobility: {mobility}\n Range: {shooting_range} \n Control: {controls}
""")
break发布于 2022-03-17 06:28:36
您需要使用split()方法来代替attachments变量。
attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
attachment = str(input("What attachment would you like to add? ")).lower() 另外,如果你的情况需要像这样
if attachment == attachments[0]:
damage += 5
mobility-= 5发布于 2022-03-17 07:00:38
始终在循环之外请求输入。如果出现错误,请继续使用continue。向if var == something of if var in something查询。
damage = 49
fire_rate = 50
accuracy = 69
mobility = 59
shooting_range = 56
controls = 53
attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
print(f"Options are {attachments}")
attachment = (input("What attachment would you like to add? ")).lower()
while True:
if attachment not in attachments:
attachment = (input("Incorrect Option.What attachment would you like to add? ")).lower()
continue
elif attachment in attachments[0]:
damage += 5
mobility -= 5
elif attachment in attachments[1]:
mobility += 5
accuracy -= 8
elif attachment in attachments[2]:
accuracy += 5
controls += 5
elif attachment in attachments[3]:
controls += 10
mobility += 2
print(f"""\n Damage: {damage}\n Fire Rate: {fire_rate}\n Accuracy: {accuracy} \n Mobility: {mobility}\n Range: {shooting_range} \n Control: {controls}
""")
breakhttps://stackoverflow.com/questions/71507735
复制相似问题