作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习 python,需要帮助理解我做错了什么。我试图完成的任务是创建一个从 1 到 100 的整数列表,然后根据这些数字创建另外两个列表,并通过打印这两个列表来完成。但是,我无法将第二个列表写入名为 target list.txt 的文件。有人能告诉我我做错了什么吗?
num1 = input("Please Input a name for the file ending it with .txt. ")
numb_list = open(num1, "w")
mylist = []
def integer_list():
for numbers in range(1, 101):
mylist.append(numbers)
numb_list.write(str(numbers) + "\n")
print(numbers, end="\n")
integer_list()
print(mylist)
numb_list.close()
def target_list():
for numbers2 in range(25, 75):
mylist.append(numbers2)
target_list.write(numbers2+ "\n")
print(numbers2, end="\n")
target_list()
print(mylist)
target_list.close()
最佳答案
target_list
而不是一个新文件。这就是导致您的程序失败的原因。 return
每次通话的新列表。这可以防止函数的一次调用影响下一次调用的行为。 def integer_list(start, end):
file_name = input("Please Input a name for the file ending it with .txt. ")
num_list = []
num_file = open(file_name, "w")
for num in range(start, end):
num_list.append(num)
num_file.write(str(num) + "\n")
print(num)
num_file.close()
return num_list
num_list1 = integer_list(1, 101)
print(num_list1)
num_list2 = integer_list(25, 75)
print(num_list2)
关于关于练习题的Python初学者问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64996500/
这是一道练习题,我遇到了一些困难: struct bodytp // Is there an error? { char *name; // If so, fix the error.
所以,我目前正在阅读 Bjarne Stroustrup 的“编程:使用 C++ 的原理和实践”,我正在阅读第 3 章。书中有一个问题是“编写一个程序,提示用户输入 3 个字符串值,然后按逗号分隔的顺
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
题目:来自Madrid且订单数少于3的消费者 建表: 复制代码代码如下: set nocount on --当 SET NOCOUNT 为 ON 时,不返回
“OCP Java SE 6 程序员实践考试(考试 310-065)”评估测试 2 中的一道题。 给定: public class WeatherTest { static Weather w; pu
我是一名优秀的程序员,十分优秀!