2017-08-08 74 views
-4

我想要做的是我自己寫我的第一個程序。我不想要答案,但只是一些指導已經兩天了,我還沒有取得任何實際進展。我是一個新手,所以對我很容易。代碼不會產生錯誤,但運行,我知道它不正確

四名同事拼車上班每天。隨機選擇一個驅動程序讓驅動器工作,然後隨機選擇驅動器回家。每個司機都有一隻腳,並且每個人都有機會獲得超速駕駛。駕駛員A每次開車時有10%的機會獲得機票,駕駛員B有15%的機會,駕駛員C有20%的機會,駕駛員D有25%的機會。國家將立即撤銷其第三張車票後的駕駛執照,一旦駕駛執照被吊銷,司機將停止在拼車區駕駛。由於只有一個警察的拼車路線上,最大的一票將每早發和一個的最大每晚上

import random 

day_counter = 0 
alan_tickets = 0 
betty_tickets = 0 
charles_tickets = 0 
diana_tickets = 0 

drivers = ["Alan", "Betty", "Charles", "Diana"] 

#def head_to_work(): is the driver selection process when heading to work. 
def head_to_work(): 
    if random.choice(drivers) == "Alan": 
     print "Alan it's your turn." 
     global alan_tickets 
     if alan_tickets == 3: 
      print "i cant" 
      head_to_work() 
     else: 
      return alan_drives_tw() 
    elif random.choice(drivers) == "Betty": 
     print "Betty it's your turn." 
     global betty_tickets 
     if betty_tickets == 3: 
      print "nope" 
      head_to_work() 
     else: 
      return betty_drives_tw() 
    elif random.choice(drivers) == "Charles": 
     print "Charles it's your turn." 
     global charles_tickets 
     if charles_tickets == 3: 
      print "no way" 
      head_to_work() 
     else: 
      return charles_drives_tw() 
    elif random.choice(drivers) == "Diana": 
     print "Diana it's your turn." 
     global diana_tickets 
     if diana_tickets == 3: 
      print "sorry guys" 
      head_to_work() 
     else: 
      return diana_drives_tw() 
    else: 
     print "All drivers have their Licenses suspended." 
     print "Take the Bus." 

# driver alan is heading to work he has a 10% chance of getting a ticket 
def alan_drives_tw(): 
    global alan_tickets 
    print "Yo i'm driving" 
    print "..." 
    print "Now driving" 
    print "..." 
    print "your getting pulled over" 
    if random.random <= 0.10: 
     print "your getting a ticket" 
     alan_tickets += 1 
     print "i got a ticket, but we have arrived at work" 
     head_home() 
    else: 
     print "just a warning today" 
     print "we have arrived at work" 
     head_home() 

# driver betty is heading to work she has a 15% chance of getting a ticket 
def betty_drives_tw(): 
    global betty_tickets 
    print "Hi i'll drive" 
    print "..." 
    print "we outta here" 
    print "your getting pulled over" 
    if random.random() <= 0.15: 
     print "your getting a ticket" 
     betty_tickets += 1 
     print "i got a ticket but, made it to work" 
     head_home() 
    else: 
     print "just a warning today" 
     print "made it to work" 
     head_home() 

#driver charles is heading to work he has a 20% chance of getting a ticket 
def charles_drives_tw(): 
    global charles_tickets 
    print "I'll take the wheel" 
    print "..." 
    print "lets roll out" 
    print "your getting pulled over" 
    if random.random() <= 0.20: 
     print "your getting a ticket" 
     charles_tickets += 1 
     print "i got a ticket but, made it to work" 
     head_home() 
    else: 
     print "just a warning today" 
     print "made it to work" 
     head_home() 

#driver charles is heading to work she has a 25% chance of getting a ticket 
def diana_drives_tw(): 
    global diana_tickets 
    print "I got it today" 
    print "..." 
    print "whippin it" 
    print "your getting pulled over" 
    if random.random() <= 0.25: 
     print "its ticket time" 
     diana_tickets += 1 
     print "i got a ticket but, were here at work" 
    else: 
     print "just a warning today" 
     print "were here at work" 
     return head_home() 


#def head_home(): is the driver selection process when heading home 
def head_home(): 
    if random.choice(drivers) == "Alan": 
     print "Alan it's your turn" 
     global alan_tickets 
     if alan_tickets == 3: 
      print "i cant" 
      return head_home() 
     else: 
      return alan_drives_h() 
    elif random.choice(drivers) == "Betty": 
     print "Betty it's your turn" 
     global betty_tickets 
     if betty_tickets == 3: 
      print "nope" 
      return head_home() 
     else: 
      return betty_drives_h() 
    elif random.choice(drivers) == "Charles": 
     print "Charles it's your turn" 
     global charles_tickets 
     if charles_tickets == 3: 
      print "no way" 
      return head_home() 
     else: 
      return charles_drives_h() 
    elif random.choice(drivers) == "Diana": 
     print "Diana it's your turn" 
     global diana_tickets 
     if diana_tickets == 3: 
      print "sorry guys" 
      return head_home() 
     else: 
      return diana_drives_h() 
    else: 
     print "Drivers are not eligible to drive" 

# driver alan is heading to work he has a 10% chance of getting a ticket 
def alan_drives_h(): 
    global alan_tickets 
    global day_counter 
    print "Yo i'm driving" 
    print "..." 
    print "Now driving" 

    print "your getting pulled over" 
    if random.random <= 0.10: 
     print "your getting a ticket" 
     alan_tickets += 1 
    else: 
     print "just a warning today" 
     print "were home" 
     day_counter += 1 
     head_to_work() 

# driver betty is heading to work she has a 15% chance of getting a ticket 
def betty_drives_h(): 
    global betty_tickets 
    global day_counter 
    print "Hi i'll drive" 
    print "..." 
    print "we outta here" 
    print "your getting pulled over" 
    if random.random() <= 0.15: 
     print "your getting a ticket" 
     betty_tickets += 1 
    else: 
     print "just a warning today" 
     print "made it home" 
     day_counter += 1 
     head_to_work() 

# driver charles is heading to work he has a 20% chance of getting a ticket 
def charles_drives_h(): 
    global charles_tickets 
    global day_counter 
    print "I'll take the wheel" 
    print "..." 
    print "lets roll out" 
    print "your getting pulled over" 
    if random.random() <= 0.20: 
     print "your getting a ticket" 
     charles_tickets += 1 
    else: 
     print "just a warning today" 
     print "made it home guys" 
     day_counter += 1 
     head_to_work() 

# driver diana is heading to work she has a 25% chance of getting a ticket 
def diana_drives_h(): 
    global diana_tickets 
    global day_counter 
    print "I got it today" 
    print "..." 
    print "whippin it" 
    print "your getting pulled over" 
    if random.random() <= 0.25: 
     print "its ticket time" 
    else: 
     print "just a warning today" 
     print "were home everyone" 
     day_counter += 1 
     head_to_work() 

print head_to_work() 
print "Alan %d tikets." % (alan_tickets) 
print "Betty %d tickets." % (betty_tickets) 
print "Charles %d tickets." % (charles_tickets) 
print "Diana %d tickets." % (diana_tickets) 
print "%d days has passed." % (day_counter) 

有,我遇到了一些問題。

  1. 獲得代碼繼續運行,直到每個人都有3票

  2. 有時停止後1,2,3,或4天最大,我不知道爲什麼

在此先感謝記住沒有答案只是線索和指導

+2

對於每個驅動程序,您確實應該使用列表而不是單獨的變量和函數,因此您沒有太多重複的代碼。 – Barmar

+0

這實際上是一個'班級司機'的好申請,你可以在這裏獲得一張車票的概率和駕駛員獲得的車票數量。 – Barmar

+0

當你遞歸地調用'head_to_work()'時,你不會返回值,它應該是'return head_to_work()'。或者使用循環代替遞歸。 – Barmar

回答

0

當您從列表中選擇一個隨機項目,並將其與多個值進行比較時,應該只進行一次隨機選擇。

if random.choice(drivers) == "Alan": 
    ... 
elif random.choice(drivers) == "Betty": 

這將選擇一個隨機驅動程序並將其與'Alan'進行比較。如果它是Alan,則if塊正確運行。如果失敗,它現在再次調用random.choice函數,並選擇一個新的隨機驅動程序與「Betty」進行比較。但是,那個隨機的司機可能是艾倫(或任何人)。

你想要更多的東西是這樣的:

driver = random.choice(drivers) 
if driver == 'Alan': 
    ... 
elif driver == 'Betty': 
    ... 

其他一些最新的想法:

打印語句來調試程序的好辦法 - 但是這與你有儘可能多的打印垃圾郵件幾乎是不可能的。在程序起作用之前註釋大部分內容 - 並添加打印語句以顯示不同時間的變量。

您有幾個拼寫錯誤,例如random.random而不是random.random()以及您忘記更新計數器的功能。這主要是由於具有很多類似的功能。隨着你繼續編寫程序,你將要學習如何創建一個單一功能來處理單次旅程中的四個驅動程序中的任何一個。編程會更容易,並減少一些拼寫錯誤。

0

你的代碼有一個基本缺陷。在您的head_to_work函數random.choice(drivers)存在於每個ifelif子句中,這是不可預測性的地方。你應該像這樣改進。

drivers = ["Alan", "Betty", "Charles", "Diana"] 
available_drivers=drivers[:] 
def head_to_work(): 
    if available_drivers: 
     selected_driver=random.choice(available_drivers) 
    else: 
     selected_driver= None 
    if selected_driver == "Alan": 
     print "Alan it's your turn." 
     global alan_tickets 
     if alan_tickets == 3: 
      available_drivers.remove("Alan") 
      print "i cant" 
      head_to_work() 
     else: 
      return alan_drives_tw() 
    #Similiarly for the other three. 
    else: 
     print "All drivers have their Licenses suspended." 
     print "Take the Bus." 

我們在這裏所做的是刪除了不必要的隨機選擇。解決了不可預測性問題。同時維護一個avaliable_drivers列表有助於找到非票價驅動程序,這是head_to_work函數中的else子句必不可少的條件。因爲當所有驅動程序獲得票款時,random.choice將獲得一個空列表。這將工作,直到所有的司機會得到票。

+0

當我這樣做時,我得到一個索引超出範圍錯誤。當所有驅動程序已從列表中刪除時,我如何才能停止查找驅動程序? –

+0

@Louis_Louisius按照上述相應地更改它。 –

相關問題