首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果我在python中输入多个附件,我的代码仍然无法工作。

如果我在python中输入多个附件,我的代码仍然无法工作。
EN

Stack Overflow用户
提问于 2022-03-17 06:08:39
回答 2查看 33关注 0票数 0

我的代码现在是工作的,但只有当我输入1值,但如果我输入2个值,它将无法工作。示例(我将输入monolithic_suppressor;owc_skeleton_stock)编译器不会读取它。我想要输入多个附件,这些附件添加或减去下面的man统计数据属性。

代码语言:javascript
复制
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
EN

回答 2

Stack Overflow用户

发布于 2022-03-17 06:28:36

您需要使用split()方法来代替attachments变量。

代码语言:javascript
复制
attachments = "monolithic_suppressor;owc_skeleton_stock;owc_laser_tactical;operator_foregrip".split(";")
attachment = str(input("What attachment would you like to add? ")).lower() 

另外,如果你的情况需要像这样

代码语言:javascript
复制
if attachment == attachments[0]:
    damage += 5
    mobility-= 5
票数 0
EN

Stack Overflow用户

发布于 2022-03-17 07:00:38

始终在循环之外请求输入。如果出现错误,请继续使用continue。向if var == something of if var in something查询。

代码语言:javascript
复制
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}
        """)
        break
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71507735

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档