我写了两个不同的序列,如何使它们按顺序打印?我需要使用循环吗?如果是这样,我该怎么做?
name = input("Who Goes There?")
print("Ah" , name , "-I'll Call You Ted Instead")
water = input("Ted, Dont You Miss The Water?")
seagulls = input("What About The Seagulls?")
print(water, seagulls)
答案 0 :(得分:1)
很难弄清楚您要问什么,特别是因为该代码中没有语法错误。我假设您正在使用Python解释器运行它,是的(1)?
在任何情况下,如果您想重复执行多个操作,则只需要循环即可。如果您只想按顺序执行一些语句,则按照所需的顺序编写它们,就像下面的记录所示:
pax:~> python3 testprog.py
Who Goes There?pax
Ah pax -I'll Call You Ted Instead
Ted, Dont You Miss The Water?yes
What About The Seagulls?no
yes no
如果这些操作的顺序与您所需的顺序不同,只需将语句重新排列为您要做的所需的顺序。
(1)我问的原因是,例如,如果您尝试使用shell运行它,则会执行出现语法错误:
pax:~> bash testprog.py
testprog.py: line 1: syntax error near unexpected token `('
testprog.py: line 1: `name = input("Who Goes There?")'
但是,在这种情况下,您只是在使用错误的工具来运行它(可能是隐含的,如果它是可执行文件,而您只是使用./testprog.py
来运行它。在这种情况下,请指定其他解释器,例如(实际行在您的系统上可能不同,但概念相同):
#!/usr/bin/env python3
name = input("Who Goes There?")
print("Ah" , name , "-I'll Call You Ted Instead")
water = input("Ted, Dont You Miss The Water?")
seagulls = input("What About The Seagulls?")
print(water, seagulls)
答案 1 :(得分:0)
如果您编写脚本-然后运行整个脚本-订单应自上而下执行。
name = input("Who Goes There?")
print("Ah" , name , "-I'll Call You Ted Instead")
water = input("Ted, Dont You Miss The Water?")
seagulls = input("What About The Seagulls?")
print(water, seagulls)
答案 2 :(得分:0)
您可以这样做。
name = input("Who Goes There?")
print(f"Ah {name} I'll call you Ted Instead")
water = input("Ted, Dont You Miss The Water?")
seagulls = input("What About The Seagulls?")
print(water, seagulls)
或者如果您愿意,也可以这样做
name = input("Who Goes There?")
print("Ah {} I'll call you Ted Instead").format(name)
water = input("Ted, Dont You Miss The Water?")
seagulls = input("What About The Seagulls?")
print(water, seagulls)