2015-10-24 20 views
-6

我想根據用戶輸入分配一個數值。在Python中爲用戶輸入分配一個值

現在我想讓用戶輸入第二位的驅動程序。輸入的任何驅動程序名將被分配2分,以完成第二。除了得到3分外,與第一名相同。

程序運行前,所有可能的驅動程序名稱都設置爲0。這些都是用戶可以輸入的名字。

我不完全相信我會在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. ")) 

我在編程方面很新,所以很抱歉,如果我沒有太大的意義。如果用戶輸入上面列出的其中一個驅動程序(例如第一名),那麼我希望該驅動程序從0到20(第一名完成的值)。

+4

如何將驅動程序名稱設置爲0?請用簡單的英語說明你正在嘗試做什麼。如問題所述,這是非常含糊的。 –

+1

首先向我們展示您的嘗試。 – blackmamba

+0

你的輸入和int調用是向後的,但是如果你運行了你輸入的內容,你就會很快知道 –

回答

0

爲什麼不把所有這些驅動程序放在一個字典中,所有的值都設置爲0?事情是這樣的:

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

如果你想改變的驅動值,你分配給它根據用戶輸入的,這裏有一個很簡單的例子:

#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) 

在這個例子中,第一個驅動程序接收20points ,試試這與你的司機名稱,它應該工作。如果您需要更多關於For循環的信息,如果語句和字典檢查Python文檔或在stackoverflow上搜索。要打印字典中某個鍵的特定值,您需要指定特定鍵,如All_Drivers["Justin_Allgaier"]。您可能還想限制用戶可以輸入的內容,但我希望此解決方案能夠爲您提供幫助。

+0

我運行了程序並選擇了Justin_Allgaier,但它從未添加過20,它保持爲0 –

+0

非常抱歉,我將修正添加到了我們不應該通過first_place_driver_name迭代的腳本,它不是可迭代的。字符串是不可迭代的,並且單個字符串不應該被迭代。只是想演示如何使用For循環給你,並有點被帶走。 –

相關問題