Python 如何使用while循环和if语句使程序重复自身?
我对Python非常陌生,我发现理解该语言的某些方面非常困难。我被要求为一个作业创建一个程序,在尝试使用这个网站几个小时后,我没有得到任何帮助 问题是: 编写一个程序,它使用一个循环来获取名字、姓氏和 来自未定义用户数的电话号码。节目 应允许用户在每次循环迭代后退出该选项程序还应将用户输入的信息写入名为names.tx的文本文件中 我对问题的粗体部分没有异议,但我真的无法理解第一点。以下是我目前掌握的情况:Python 如何使用while循环和if语句使程序重复自身?,python,if-statement,Python,If Statement,我对Python非常陌生,我发现理解该语言的某些方面非常困难。我被要求为一个作业创建一个程序,在尝试使用这个网站几个小时后,我没有得到任何帮助 问题是: 编写一个程序,它使用一个循环来获取名字、姓氏和 来自未定义用户数的电话号码。节目 应允许用户在每次循环迭代后退出该选项程序还应将用户输入的信息写入名为names.tx的文本文件中 我对问题的粗体部分没有异议,但我真的无法理解第一点。以下是我目前掌握的情况: def main(): infoList = [] count = 0
def main():
infoList = []
count = 0
while True:
firstname = input('Please enter your first name: ')
lastname = input('Please enter your last name: ')
telephoneno = input('Please enter your telephone number: ')
contiinue = input('Continue (y = yes): ')
if contiinue == y:
count = count + 1
main()
当程序再次迭代时,我希望用户能够给出问题的不同细节。我知道人们讨厌别人问问题,似乎他们只是想让别人为他们做作业,但如果我找不到解决办法,这会让我有一段时间感到困扰。这只是一个提纲 尝试以下方法:
names=[]
counter=1
while True:
name=input("name {} or 'q' to quit:".format(counter))
if name.lower()=="q":
break
else:
names.append(name)
counter+=1
print(names)
这里有几个问题:
- 首先,正如Paul McGuire的评论所说,您的while循环没有正确缩进;因此Python将其视为程序文件的一个单独部分
- 其次,你无法离开while循环。如果要使用
构造,则需要在某处使用语句;很可能,如果contiinue='y'(我最初忽略的另一个问题是您在那里漏掉了引号),您希望在while True
的
的“then block”后面加一个
,并将else
作为break
块中的唯一一行。或者,您可以在while中进行测试,当达到某个条件时,该测试将停止循环。例如,您可以将contiinue变量预初始化为“y”(在循环插入else
之前),并将contiinue='y'
更改为while True
while contiinue='y'
- A立即结束while循环。您可以决定用户应该键入什么内容退出(如
或其他任何内容),检查他们是否键入了该内容,如果是,则执行“q”
break
- 而循环在其声明中有一个退出条件,您可以将其用于与上述相同类型的退出命令。例如:
user_input = "default value" while user_input != "q": user_input = raw_input("Type 'q' if you want to exit!")
Python有一个名为的内置函数,可用于与文件交互
- 您似乎有两个不同的问题:
您有两个选择:
my_file=open(“my_file.csv”,“r”)
将在“r”模式(只读模式)下为您提供一个文件对象;您可以对my_文件中的行执行之类的操作来访问其内容
my_file=open(“my_file.csv”,“w”)
将在“w”模式(写入模式)下为您提供一个文件对象;您可以通过执行my\u file.write(my\u data)
将变量的内容写入其中。请注意,这将覆盖文件中已有的任何内容,因此请小心操作
处理完文件后,请确保使用my\u file.close()
将其关闭
祝你好运 首先需要缩进循环及其内容,以便将其识别为main()的一部分,因为它与main()是分开的
对于while循环,应使用initialize continue to'n'并按如下方式更改while循环while continue=='y'
,这将继续循环,直到输入除“y”以外的内容。如果continue==y:
也应更改为如果continue==y:
,因为不带引号的y是变量,而不是字符
至于文本文件,您应该在循环之前打开文件,在循环中写入,然后关闭它。由于您是逐行写入,因此也不需要infoList[]或count,因为将\n
添加到f的末尾。write()会向下移动该行
def main():
continue = 'n'
f = open('names.txt','w')
while continue == 'y':
firstname = input('Please enter your first name: ')
lastname = input('Please enter your last name: ')
telephoneno = input('Please enter your telephone number: ')
continue = input('Continue (y = yes): ')
f.write(firstname + ' '+ lastname + ' ' telephoneno + '\n')
f.close()
main()
我希望这样做(对代码的最小更改):
我在代码中添加了一些代码和注释,这些应该可以帮助您入门
def main():
infoList = []
count = 0
while True:
firstname = input('Please enter your first name: ')
lastname = input('Please enter your last name: ')
telephoneno = input('Please enter your telephone number: ')
# you probably want to append to infoList here
contiinue = input('Continue (y = yes): ')
if contiinue == "y": #make sure it's a string
count = count + 1
else:
# figure out how to break from your loop
# otherwise it will execute forever
#outside the loop here so write everything in infoList here
with open('names.txt', 'w') as f:
#write to your file
main()
您的意思是让while循环位于main()函数中吗?然后,它和它的主体都应该缩进以匹配赋值语句。
def main():
infoList = []
count = 0
while True:
firstname = input('Please enter your first name: ')
lastname = input('Please enter your last name: ')
telephoneno = input('Please enter your telephone number: ')
# you probably want to append to infoList here
contiinue = input('Continue (y = yes): ')
if contiinue == "y": #make sure it's a string
count = count + 1
else:
# figure out how to break from your loop
# otherwise it will execute forever
#outside the loop here so write everything in infoList here
with open('names.txt', 'w') as f:
#write to your file
main()