我想要做的是我自己寫我的第一個程序。我不想要答案,但只是一些指導已經兩天了,我還沒有取得任何實際進展。我是一個新手,所以對我很容易。代碼不會產生錯誤,但運行,我知道它不正確
四名同事拼車上班每天。隨機選擇一個驅動程序讓驅動器工作,然後隨機選擇驅動器回家。每個司機都有一隻腳,並且每個人都有機會獲得超速駕駛。駕駛員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)
有,我遇到了一些問題。
獲得代碼繼續運行,直到每個人都有3票
有時停止後1,2,3,或4天最大,我不知道爲什麼
在此先感謝記住沒有答案只是線索和指導
對於每個驅動程序,您確實應該使用列表而不是單獨的變量和函數,因此您沒有太多重複的代碼。 – Barmar
這實際上是一個'班級司機'的好申請,你可以在這裏獲得一張車票的概率和駕駛員獲得的車票數量。 – Barmar
當你遞歸地調用'head_to_work()'時,你不會返回值,它應該是'return head_to_work()'。或者使用循環代替遞歸。 – Barmar