【问题标题】:How do I use "while" in this situation在这种情况下如何使用“while”
【发布时间】:2013-09-27 18:39:01
【问题描述】:
# This program says hello and asks for my name, then repeats it.
print('Hello world!')
print('What is your name?')
myName = input()

while 
    print('It is nice to meet you, ' + myName)

我的问题是我在哪里放什么? 我正在尝试学习如何使用 while 循环,但我不知道在 while 之后放什么让它永远重复你的名字。 提前致谢!

【问题讨论】:

    标签: python while-loop python-3.3


    【解决方案1】:

    如果您想输入一个名称并无限期打印,请使用:

    print('Hello world!')
    print('What is your name?')
    myName = input()
    
    while True:
        print('It is nice to meet you, ' + myName)
    

    如果你想输入一个名字,打印出来,然后输入另一个等等,使用:

    print('Hello world!')
    while True:
        print('It is nice to meet you, ' + input('What is your name? '))
    

    【讨论】:

      【解决方案2】:

      您可以使用将始终评估为True 的任何条件。最明显(因此更可取)的方式是

      while True:
          print('It is nice to meet you, ' + myName)
      

      另请注意,您不需要将字符串连接与print()

      print('It is nice to meet you,', myName)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2015-11-30
        • 2017-05-16
        • 2012-10-06
        • 2011-05-18
        • 2019-11-29
        • 2022-01-23
        相关资源
        最近更新 更多