【问题标题】:how to run a continuous loop and add input to text如何运行连续循环并向文本添加输入
【发布时间】:2014-02-08 13:12:00
【问题描述】:

我必须运行一个连续循环,直到用户另有决定,这会将输入的输入添加到文本文件中,但我似乎无法让 while 循环正常工作。

这是我目前所拥有的:

def main():

    #Open a file named names.txt.
    outfile = open('names.txt', 'w')

    #user enters input
    fname = input("Enter first name:\t")
    lname = input("Enter second name:\t")
    telephone = input("Enter telephone number:\t")
    print("")
    continues = input("Continue? (y = yes):\t")
    input_list = [fname, lname, telephone, continues]

    outfile.write(fname)
    outfile.write(lname)
    outfile.write(telephone)

    outfile.close()

    while continues == 0:
        if continues == "y":
            fname = input("Enter first name:\t")
            lname = input("Enter second name:\t")
            telephone = input("Enter telephone number:\t")
            print("")
            continues = input("Continue?:\t")

            outfile.write(fname)
            outfile.write(lname)
            outfile.write(telephone)

            outfile.close()
        else:
            print("File Written")

#call main
main()

有人可以帮帮我吗,我正在使用 python 3.3.2

【问题讨论】:

  • 只给出代码的相关部分可能会得到更好的答案。
  • while continues == 0: - continues 什么时候会是 0?这个循环应该在什么条件下进行?
  • 我不知道我只是这样做了,作为一个例子,我有 while continue == y 并且它一直说 y was not defined...
  • y"y" 不一样。第一个是名为y 的变量(未定义),第二个是包含字符y 的字符串。

标签: python loops while-loop constants


【解决方案1】:

你最好将整个事情包装在循环中:

例如:

def main():

    while True: 

        fname = input("Enter first name:\t")
        lname = input("Enter second name:\t")
        telephone = input("Enter telephone number:\t")
        print ("")
        continues = input("Continue? (y = yes):\t").lower() # y to break the loop
        input_list = [fname, lname, telephone, continues]

        if continues == 'y':
            with open('names.txt', 'w') as outfile:
                # write things to outfile. 
                # The with block auto closes 
                # the file after it's statements
            print ('File Written')
            break # this is how you exit the loop

编辑:

也许这条线会更有意义:

continues = input("Write to File? (y = yes):\t").lower()

【讨论】:

  • 非常感谢,唯一的事情是“y”是假设让循环继续而不是打破它,但谢谢你:)
  • 很高兴我能帮上忙。正如您可能看到的那样,根据您的目的调整这部分内容很容易。
【解决方案2】:

这是一个正在工作的循环。您可以尝试一下并根据您的情况进行调整吗?

run = None    
while run != 'n':
   run = raw_input('continue? [y/n]').lower()

【讨论】:

    【解决方案3】:

    据我所知,试试吧:

    continues='y'
    while 1:
        if continues == "y":
            fname = input("Enter first name:\t")
            lname = input("Enter second name:\t")
            telephone = input("Enter telephone number:\t")
            print("")
            continues = input("Continue?:\t")
    
            outfile.write(fname)
            outfile.write(lname)
            outfile.write(telephone)
    
            outfile.close()
        else:
            print("File Written")
            break
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      • 2022-01-05
      • 2016-07-19
      • 2022-11-28
      • 1970-01-01
      • 2020-08-22
      • 2015-01-21
      相关资源
      最近更新 更多