我想学习的是如何允许用户创建名称列表,然后在每个名称上附加一个数字,以便用户以后查找。我什至不确定数据库是否是正确的术语。
我能够允许用户创建一个数字列表,但是我不知道该如何做,让用户列出一个名称列表,然后附加两个名称,以便用户可以查找名称并找到他们的姓名在创建列表后得分。
list_of_nums = []
loop = True
while loop == True:
try: num = int(input("Enter Integer, 0 to end: "))
except ValueError: num = "invalid"
if num == "invalid": print("\nPlease Print A Valid Integer!\n");
elif num != 0: list_of_nums.append(num)
else: loop = False
print(list_of_nums)
答案 0 :(得分:0)
您应该先学习泡菜:https://docs.python.org/3/library/pickle.html
一旦您有了大致的了解,请掌握sqlite的用法。 The raw sqlite please,以使自己了解SQL语言。这不是ansi sql,但很流行并且足够接近。
Then use some query builder帮助您掌握your next data ba se。
最后,关于数字,姓名和分数的问题,请尝试使用同一循环询问所有所需信息,或者尝试使用更多循环。
我觉得您正在学习python,因此您可以参加codecademy.com的python课程。这样可以避免麻烦。
答案 1 :(得分:0)
如果您只想将这些输入临时保存在列表中,则应执行以下操作:
list_of_nums = []
num = 1
while num:
try:
num = int(input("Enter a integer, 0 to end: "))
except:
print("Enter a integer number")
list_of_nums.append(num)
print(list_of_nums)
编辑:
但是,如果要配对值,最好使用字典:
grades = {}
num = 1
while True:
try:
name = input("Name: ")
num = int(input("Grade, -1 to end: ")) # the person can also take 0!
except:
print("ENTER A INTEGER NUMBER!")
continue
if num == -1:
break # break the loop
# if <num> isn't between 0 and 10:
if num < 0 or num > 10: # or the minimum and max values you want
print("ENTER A GRADE BETWEEN 0 AND 10!")
continue
grades[name] = num # You can acess <num> with grades[name]
#SHOW THE GRADES IN ALPHABETIC ORDER
grades_in_order = []
#thats how you iterate a dictionary
#you get the keys with a for, and access that key like a list index
for name in grades:
grades_in_order.append(name)
grades_in_order.sort()
print("\n\nGrades:")
for name in grades_in_order:
print(name, grades[name], sep='\t') # the sep is optional
这里是运行示例:
Name: Iago
Grade, -1 to end: Invalid
ENTER A INTEGER NUMBER!
Name: Hano
Grade, -1 to end: 11
ENTER A GRADE BETWEEN 0 AND 10!
Name: Iago
Grade, -1 to end: -2
ENTER A GRADE BETWEEN 0 AND 10!
Name: Hano
Grade, -1 to end: 10
Name: Iago
Grade, -1 to end: 10
Name: Nick
Grade, -1 to end: 0
Name: Mary
Grade, -1 to end: 9
Name: Jane
Grade, -1 to end: 7
Name: DOESNT MATTER
Grade, -1 to end: -1
Grades:
Hano 10
Iago 10
Jane 7
Mary 9
Nick 0