Python 当我运行这个时,我得到一个;无”;回答而不是我的问题
当我运行代码时,我希望用户响应“What do you want?”行。相反,输入行显示在“无”旁边。此外,PyCharm在我的def shop_界面行中还提到了“需要两个空行,但得到了0”。我包括函数的其余部分,以防我需要放低一些 我把它放在函数块中,但我想我遗漏了一些东西,我试着把你想要的输入项放到函数中,但我认为这会让事情变得更糟Python 当我运行这个时,我得到一个;无”;回答而不是我的问题,python,function,Python,Function,当我运行代码时,我希望用户响应“What do you want?”行。相反,输入行显示在“无”旁边。此外,PyCharm在我的def shop_界面行中还提到了“需要两个空行,但得到了0”。我包括函数的其余部分,以防我需要放低一些 我把它放在函数块中,但我想我遗漏了一些东西,我试着把你想要的输入项放到函数中,但我认为这会让事情变得更糟 yes = "yes" print("Welcome to Python's Shop! What can I do for you?") item_wante
yes = "yes"
print("Welcome to Python's Shop! What can I do for you?")
item_wanted = input(print("What do you want? Food? Bed?: "))
def shop_interface():
gold = 200
if item_wanted == "food":
print("I have some apples to buy! 20G Each!")
input("Want them?:")
if yes:
gold = gold - 20
print("You have " + str(gold) + "G left")
elif item_wanted == "bed":
print("I have a room for about 15G...want it?")
elif item_wanted == "weapon":
print(" There's an old sword in the back, I'll give it to you for 100G.")
else:
print("Don't have that, we have food and bed though!")
我希望输入行位于“您想要什么?食物?床?”行的旁边,而不是不显示。谢谢你的帮助 汤姆·卡泽斯是对的;您应该将
input(print(“…”)
更改为input(“…”)
。您当前的操作方式使用打印输出,即None
。您的代码当前相当于input(None)
,这就是为什么您会得到如此奇怪的None
。正确的代码应该是输入(“你想要什么?食物?床?:”)
你有这个
item_wanted = input(print("What do you want? Food? Bed?: "))
但这是行不通的,因为print
不会返回任何内容。它只打印到sys.stdout
(它也可以打印到流)您传递给它的任何内容
事实上,你应该这样做
item_wanted = input("What do you want? Food? Bed?: ")
将输入(打印(“…”)更改为输入(“…”)
。input
函数接受一个字符串参数,但您要将print
的返回值传递给它,即None
。