使用input()函数在字符串迭代旁边打印变量

时间:2019-06-23 14:27:24

标签: python io

我想向用户提出一个问题,其中包括python中已经输入的单词。怎么做?

live = input("Where do you live?")
print("You live in ", live,".")

place = input("Where is", live,"?")
print("Oh! so it's in", place, ".")

我已经尝试输入变量并将其单独保存。但是,它会给原始输入中输入的参数带来错误。

TypeError: raw_input() takes from 1 to 2 positional arguments but 3 were given

4 个答案:

答案 0 :(得分:4)

使用format():-

live = input("Where do you live?")
print("You live in ", live,".")

place = input("Where is {} ?".format(live))
print("Oh! so it's in", place, ".")

答案 1 :(得分:0)

不能将变量放在raw_input()input()中。

因此,您不能在liveinput("x",live,"y")之间加入

您只需要编写一条语句即可。

所以会是这样。

live = input("Where do you live?")

print("You live in ", live,".")

place = input("Where is it?")

print("Oh! so it's in", place, ".")

答案 2 :(得分:0)

尝试一下

live = input("Where do you live?")
print("You live in ", live,".")
print("\n Where is ", live)
place = input()
print("Oh! so it's in", place, ".")

样品运行

Where do you live?los angeles
You live in  los angeles .
Where is  los angeles
africa
Oh! so it's in africa .

答案 3 :(得分:0)

1)在input方法中,不能直接将字符串和变量一起使用。您必须在format

的帮助下使用它们
live = input("Where do you live?")
print("You live in ", live,".")

place = input(f"Where is {live} ?")  # Here f indicates format
print("Oh! so it's in", place, ".")

希望对您有帮助。