简体   繁体   English

您如何连接输入?

[英]How do you concatenate in an input?

import msvcrt as m
def wait():
    m.getch()
strength = 10
health = 10
input("You now have",strength,"strength and",health,"health")
wait()

This is a slice of a project i'm working on and I have come across a seemingly unsolvable issue. 这是我正在从事的项目的一部分,我遇到了一个看似无法解决的问题。 When this code is run, it should print ("You have now 10 strength and 10 health") but this is returned: 运行此代码时,它应该打印(“您现在具有10点力量和10点生命值”),但返回:

input("You now have",strength,"strength and",health,"health")
TypeError: input expected at most 1 arguments, got 5

I am absolutely baffled and I have tried many different things to fix this including adjusting the variables like: str(strength), (strength) and I tried replacing the commas with '+' but nothing seems to work and it stops my program from working properly. 我绝对感到困惑,我尝试了许多不同的方法来解决此问题,包括调整变量,例如:str(strength),(strength),并尝试用'+'替换逗号,但似乎无济于事,它使程序无法正常工作正确地。

As the commenter khelwood pointed out, you can't pass multiple parameters to the function input . 正如评论者khelwood指出的那样,您不能将多个参数传递给函数input However, adding your variables strength and health to strings won't directly work since the variables are of type int. 但是,由于变量的类型为int,因此无法直接将变量的强度和运行状况添加到字符串中。 So, try the following. 因此,请尝试以下操作。

import msvcrt as m
def wait():
    m.getch()
strength = 10
health = 10
input("You now have "+str(strength)+" strength and "+str(health)+" health")
wait()

Hope that helped! 希望能有所帮助!

Try something like this: 尝试这样的事情:

inputString = "You now have " + str(strength) + " strength and " + str(health) + " health"
input(inputString)

I am assuming that you want to output the data that you are currently passing to input . 我假设您要输出当前传递给input

You can use string formatting in a print function to do so: 您可以在print功能中使用字符串格式:

print('You now have {} strength and {} health'.format(strength, health))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM