【发布时间】:2021-01-26 01:44:17
【问题描述】:
我创建了this Google Colab 笔记本以从表单输入中获取用户数据:
there_is_more_people = True
people = []
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def register_people(there_is_more_people):
while there_is_more_people:
did_you_finish = input("did you finish filling the fields? y/n")[0].lower()
#@markdown ## Fill your data here:
name = "mary" #@param {type:"string"}
age = 21#@param {type:"integer"}
new_person = Person(name, age)
people.append(new_person)
if did_you_finish == 'y':
people.append(new_person)
input_there_is_more_people = input("there is more people? y/n")[0].lower()
if input_there_is_more_people == 'y':
there_is_more_people = True
name = None
age = None
else:
there_is_more_people = False
for i in people:
print("These are the people you registered:")
print(i.name)
register_people(there_is_more_people)
预期的行为是,在填写一个人的数据并将其添加到对象列表(人)之后,用户将能够更改表单的数据并向其中添加另一个对象。 Ex(在通知 'mary', 21 e 'john', 20 之后):
print("These are the people you registered:")
for i in people:
print(i.name, i.age)
These are the people you registered:
mary, 21
john, 20
但是,即使修改输入数据:
#@markdown ## Fill your data here:
name = "mary" #@param {type:"string"}
age = 21#@param {type:"integer"}
#@markdown ## Fill your data here:
name = "john" #@param {type:"string"}
age = 20#@param {type:"integer"}
我只得到单元格开始执行时表单上的值:
These are the people you registered:
mary, 21
mary, 21
如何循环并获取实际的表单填充数据?
【问题讨论】:
标签: python jupyter-notebook google-colaboratory