Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Coursework
Views: 66
import random global chanceoffarleft global chanceoffarright global chanceofcentreleft global chanceofcentreright global averageelectorate global seatsinparliament chanceoffarleft = 0.1 chanceoffarright = 0.1 chanceofcentreleft = 0.3 chanceofcentreright = 0.3 averageelectorate = 700 seatsinparliament = 650 def floatingvote(): r = random.random() if 0<=r<chanceoffarleft: return "farleft" elif chanceoffarleft<=r<(chanceoffarleft + chanceoffarright): return "farright" elif (chanceoffarleft + chanceoffarright)<=r<(chanceoffarleft + chanceoffarright + chanceofcentreleft): return "centreleft" elif (chanceoffarleft + chanceoffarright + chanceofcentreleft)<=r<(chanceoffarleft + chanceoffarright + chanceofcentreleft + chanceofcentreright): return "centreright" elif (chanceoffarleft + chanceoffarright + chanceofcentreleft + chanceofcentreright)<=r<1: return "disenfranchised" def constituencyelection(): votescast = 0 farleftvote = 0 farrightvote = 0 centreleftvote = 0 centrerightvote = 0 spoiltballots = 0 while votescast < averageelectorate: v = floatingvote() if v == "farleft": farleftvote+=1 elif v == "farright": farrightvote+=1 elif v == "centreleft": centreleftvote+=1 elif v == "centreright": centrerightvote+=1 elif v == "disenfranchised": spoiltballots+=1 votescast+=1 if farleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "farleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif farrightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "farright victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif centreleftvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "centreleft victory", farleftvote, farrightvote, centreleftvote, centrerightvote elif centrerightvote == max(farleftvote, farrightvote, centreleftvote, centrerightvote): return "centreright victory", farleftvote, farrightvote, centreleftvote, centrerightvote def generalelection(numberofelections = 4): global chanceoffarleft global chanceoffarright global chanceofcentreleft global chanceofcentreright global averageelectorate global seatsinparliament while numberofelections > 0: seatsdecided = 0 farleftseats = 0 farrightseats = 0 centreleftseats = 0 centrerightseats = 0 totalfarleftvote = 0 totalfarrightvote = 0 totalcentrerightvote = 0 totalcentreleftvote = 0 while seatsdecided < seatsinparliament: s = constituencyelection() totalfarleftvote += s[1] totalfarrightvote += s[2] totalcentreleftvote += s[3] totalcentrerightvote += s[4] if s[0] == "farleft victory": farleftseats+=1 if s[0] == "farright victory": farrightseats+=1 if s[0] == "centreleft victory": centreleftseats+=1 if s[0] == "centreright victory": centrerightseats+=1 seatsdecided+=1 if farleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats): return "farleft government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, farleftseats, farrightseats, centreleftseats, centrerightseats if farrightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats): return "farright government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, farleftseats, farrightseats, centreleftseats, centrerightseats if centreleftseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats): return "centreleft government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, farleftseats, farrightseats, centreleftseats, centrerightseats if centrerightseats == max(farleftseats, farrightseats, centreleftseats, centrerightseats): return "centreright government", totalfarleftvote, totalfarrightvote, totalcentreleftvote, totalcentrerightvote, farleftseats, farrightseats, centreleftseats, centrerightseats chanceoffarleft = (2/3) * (totalfarleftvote / (averageelectorate * seatsinparliament)) chanceoffarright = (2/3) * (totalfarrightvote / (averageelectorate * seatsinparliament)) chanceofcentreleft = (totalcentreleftvote / (averageelectorate * seatsinparliament)) chanceofcentreright = (totalcentrerightvote / (averageelectorate * seatsinparliament)) numberofelections -= 1 generalelection(5)
('centreleft government', 45300, 45686, 136720, 136461, 0, 0, 334, 316)