Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用字典创建条件?_Python_Python 3.x_Dictionary_Conditional Statements - Fatal编程技术网

Python 如何使用字典创建条件?

Python 如何使用字典创建条件?,python,python-3.x,dictionary,conditional-statements,Python,Python 3.x,Dictionary,Conditional Statements,如果这听起来不复杂,很抱歉,但我是Python新手 如果一个孩子的分数大于或等于100,他们应该得到8份礼物;如果一个孩子的分数在50到100之间,他们会得到5份礼物;如果一个孩子的分数低于50,他们会得到2份礼物 如何使用字典检查用户的输入是否正确 它将首先显示他们的分数,如: presents=[] People={"Dan":22, "Matt":54, "Harry":78, "Bob":91} def displayMenu():

如果这听起来不复杂,很抱歉,但我是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()
presents=[]
人={“丹”:22,
“马特”:54岁,
“哈利”:78,
“鲍勃”:91}
def displayMenu():
打印(“1.添加礼物”)
打印(“2.退出”)
choice=int(输入(“输入您的选择:”)
而2
与所问问题相关:

如果一个孩子的分数大于或等于100,他们应该得到8份礼物;如果一个孩子的分数在50到100之间,他们会得到5份礼物;如果一个孩子的分数低于50,他们会得到2份礼物

创建一个字典,其中键是分数的布尔条件。但是字典每次都必须重新创建,因为
score
在它的键中使用,并且每个循环的分数可能不同

>>> score = 3
>>> presents = {score < 50: 2,
...             50 <= score < 100: 5,
...             score >= 100: 8
...             }[True]
>>> presents
2
>>> # and keep re-doing the 3 statements for each score.
并像这样使用它:

>>> get_presents(25)
2
>>> get_presents(50)
5
>>> get_presents(100)
8
>>>
至于您使用的代码,这似乎与问题的意图相去甚远

或者,您可以按以下键执行
int(score/50)

>>> present_dict = {1: 2,
...                 2: 5,
...                 3: 8
...                 }
>>>
>>> score = 500
>>> present_dict[min(int(score / 50) + 1, 3)]  # needed to not go over 3
8
>>> score = 25
>>> present_dict[min(int(score / 50) + 1, 3)]  # the +1 needed since it's int div
2
>>> # or put the expression above in the `get_presents` function and return that

问题是什么?现在是什么?你想给孩子多少礼物?如果是这样的话,那么如果你有预定数量的礼物给他们,为什么会有呢?你问的不是很清楚。再看一看你的问题,并试图清楚地重申你的问题所在。如果礼物数量由孩子的分数决定,为什么你要求用户输入你想要添加的礼物数量:?重复
>>> present_dict = {1: 2,
...                 2: 5,
...                 3: 8
...                 }
>>>
>>> score = 500
>>> present_dict[min(int(score / 50) + 1, 3)]  # needed to not go over 3
8
>>> score = 25
>>> present_dict[min(int(score / 50) + 1, 3)]  # the +1 needed since it's int div
2
>>> # or put the expression above in the `get_presents` function and return that