简体   繁体   English

在 Python 中为用户输入赋值

[英]Assign A Value To User Input In Python

I would like to assign a numeric value dependent on user input.我想根据用户输入分配一个数值。

Now would I would like is for the user to type in the 2nd place driver.现在我想让用户输入第二名的驱动程序。 What ever driver name is typed in would be assigned 2 points for finishing second.输入任何驾驶员姓名将获得 2 分以完成第二名。 Same deal with first place except they get 3 points.与第一名相同,但他们得到 3 分。

All possible drivers names are set to 0 before the program runs.在程序运行之前,所有可能的驱动程序名称都设置为 0。 Those are the possible names the user can type in.这些是用户可以输入的可能名称。

I'm not exactly sure I would do this in Python, and help would be appreciated.我不确定我会在 Python 中做到这一点,我们将不胜感激。

Matt_Kenseth = 0
Kyle_Busch = 0
Joey_Logano = 0
Aric_Armirola = 0
Dale_Earnhardt_Jr = 0
Denny_Hamlin = 0
Jeff_Gordon = 0
Brad_Keselowski = 0
Jimmie_Johnson = 0
Clint_Bowyer = 0
Carl_Edwards = 0
Kyle_Larson = 0
Jamie_McMurray = 0
Kevin_Harvick = 0
Kurt_Busch = 0
Ricky_Stenhouse_Jr = 0
David_Ragan = 0
Kasey_Kahne = 0
Danica_Patrick = 0
Ryan_Newman = 0
Casey_Mears = 0
Brian_Scott = 0
Trevor_Bayne = 0
AJ_Allmendinger = 0
Justin_Allgaier = 0
Paul_Menard = 0
Austin_Dillon = 0
Sam_Hornish_Jr = 0
Tony_Stewart = 0
Landon_Cassill = 0
Greg_Biffle = 0
Martin_Truex_Jr = 0
David_Gilliland = 0
JJ_Yeley = 0
Brett_Moffitt = 0
Matt_DiBenedetto = 0
Alex_Bowman = 0
Cole_Whitt = 0
Jeb_Burton = 0
Jeffrey_Earnhardt = 0
Reed_Sorenson = 0
Michael_McDowell = 0
Michael_Annett = 0

int ( input ( "Enter 1st Place Driver In Race.\nThis driver will receive 20 points. " ) )

int ( input ( "Enter 2nd Place Driver In Race.\nThis driver will receive 10 points. " ) )

int ( input ( "Enter 3rd Place Driver In Race.\n This driver will receive 5 points. " ) ) 

I'm very new at programming so sorry if I'm not making much sense.我对编程很陌生,如果我没有多大意义,我很抱歉。 If the user types in one of the drivers listed above for example for 1st place, then I would like that driver to go from 0 to 20 (the value for a 1st place finish).如果用户输入上面列出的驱动程序之一,例如第 1 名,那么我希望该驱动程序从 0 到 20(第 1 名完成的值)。

Why don't you put all those drivers in a Dictionary with all the values set to 0 ?你为什么不把所有这些驱动程序都放在一个字典中,所有的值都设置为 0 ? Something like this:像这样的东西:

All_Drivers = {"Justin_Allgaier": "0", "Jeb_Burton" : "0"}

If you want to change the drivers value you assigned to it based on the user input,here's a very simple example:如果您想根据用户输入更改分配给它的驱动程序值,这是一个非常简单的示例:

#Assigning driver names in a dictionary
All_Drivers = {"Justin_Allgaier": "0", "Jeb_Burton" : "0"}

#We're attributing a variable to the user input here
first_place_driver_name = input("Enter 1st place driver\n>")

#Here the for loop is itterating through all the keys in the dictionary and assigning all the keys to the "keys" variable, we can also assign all the values by passing an extra variable
for keys in All_drivers:
    #Now we're using an If statement to check if the name passed by the user matches a key in the dictionnary, if it does, it assigns the value 20 to the correct key.
    if first_place_driver_name == keys:
        All_Drivers[keys] = 20
#Now we're printing all the keys and values in the dictionary.
print(All_Drivers)

In this example the first driver received 20points, Try this out with your Driver names, it should work.在这个例子中,第一个司机得到 20 分,用你的司机名字试试这个,它应该可以工作。 If you need more information about For loops, If statements and dictionaries check out the Python documentation or search on stackoverflow.如果您需要有关 For 循环、If 语句和字典的更多信息,请查看 Python 文档或在 stackoverflow 上搜索。 To print out a specific value of a key in your dictionary you would need to specify the specific key like this All_Drivers["Justin_Allgaier"] .要打印字典中键的特定值,您需要指定特定键,如All_Drivers["Justin_Allgaier"] You might also want to limit what the user can input but i hope this solution can help you.您可能还想限制用户可以输入的内容,但我希望此解决方案可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM