【问题标题】:Input ( ) problems [duplicate]输入()问题[重复]
【发布时间】:2018-12-20 02:49:20
【问题描述】:

我的代码是:

print("you slowly open your eyes and look around.\n")
print
print("The room you are in has a chest of drawers in the corner, a nightstand, and a loose floorboard.\n")
print
place = input ('What do you examine first: ' )
print("you inspect the place. Underneath you find a small stick.")

不幸的是,当我测试它时,最后一个打印序列显示:

你检查这个地方。在下面你会发现一根小棍子

而不是说:

您检查地板/橱柜/床头柜。在下面你会发现一根小棍子。

我做错了什么?

【问题讨论】:

    标签: python-3.x


    【解决方案1】:

    你最后一行的“地方”只是一个字符串,不是你之前得到的变量,你需要将变量注入整个字符串。

    如下:

    place = input ('What do you examine first: ' )
    print("you inspect the %s. Underneath you find a small stick." % place)
    

    更多方法,参考this

    【讨论】:

    • % 是一个非常古老的语法。考虑切换到"you inspect the {}. ...".format(place)f"you inspect the {place}..."
    【解决方案2】:

    使用它,它会工作

    print(f"you inspect the {place}. Underneath you find a small stick.")
    

    【讨论】:

    • 谢谢。我已经阅读了 Python 的基础知识并且我非常熟练,但是我上次使用它已经有好几年了,并且有一些事情发生了变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多