抱歉,如果这听起来很简单,但我是新的Python。你知道吗
如果一个孩子的分数大于或等于100,他们应该得到8份礼物;如果一个孩子的分数在50到100之间,他们得到5份礼物;如果一个孩子的分数低于50,那么他们得到2份礼物。你知道吗
如何使用词典检查用户的输入是否正确?你知道吗
它将首先显示他们的分数,如:
presents=[]
People={"Dan":22,
"Matt":54,
"Harry":78,
"Bob":91}
def displayMenu():
print("1. Add presents")
print("2. Quit")
choice = int(input("Enter your choice : "))
while 2< choice or choice< 1:
choice = int(input("Invalid. Re-enter your choice: "))
return choice
def addpresents():
name= input('Enter child for their score: ')
if name == "matt".title():
print(People["Matt"])
if name == "dan".title():
print(People["Dan"])
if name == "harry".title():
print(People["Harry"])
if name == "bob".title():
print(People["Bob"])
present=input('Enter number of presents you would like to add: ')
if present
#This is where I got stuck
option = displayMenu()
while option != 3:
if option == 1:
addpresents()
elif option == 2:
print("Program terminating")
option = displayMenu()
与所问问题相关:
创建一个字典,其中键是分数的布尔条件。但是字典每次都必须重新创建,因为它的键中使用了
score
,而且每个循环的分数可能不同。你知道吗实际上,每个字典键都变成
True
或False
,其中只有一个值是True
。因此,获取键True
的值给出礼物的数量。你知道吗将其应用到函数中更有意义,比如:
像这样使用:
至于您使用的代码,这似乎与问题的意图相去甚远。你知道吗
或者,您可以使用
int(score/50)
作为键:相关问题 更多 >
编程相关推荐