Case:
Buat Program Utama yang memanggil kelas msdie.py dengan
1. Input: Nilai awal dari 2 Dadu
2. Proses: Roll 2 dadu tersebut
3. Output: Jumlah dari nilai 2 dadu tersebut
Answer:
from msdie import MSDie
def main():
try:
d1 = input(“Masukkan jumlah sisi dadu pertama : “)
d2 = input(“Masukkan jumlah sisi dadu kedua : “)
dadu1 = MSDie(d1)
dadu2 = MSDie(d2)
dadu1.roll()
dadu2.roll()
print “\nDadu di roll…\n”
print “Hasil acak dadu pertama : “, dadu1.getValue()
print “Hasil acak dadu kedua : “, dadu2.getValue()
print “\nHasil penjumlahan dari 2 dadu tersebut adalah : “, dadu1.getValue()+dadu2.getValue()
except Exception:
print “Input yang anda masukkan, silakan coba lagi\n”
main()
if __name__ == “__main__”: main()
this code created on python(x,y) with python version 2.7.6
And it’s for the code!
jumlah-2-dadu
from msdie import MSDie
from graphics import *
def main():
win = GraphWin(“Dice”, 350, 350)
win.setBackground(‘pink’)
Text(Point(175, 30), “Input how many sides the dices have”).draw(win)
Text(Point(110, 60), “1st Dice”).draw(win)
Text(Point(240, 60), “2nd Dice”).draw(win)
d1 = Entry(Point(110,100), 3)
d1.setText(“6”)
d1.draw(win)
d2 = Entry(Point(240,100), 3)
d2.setText(“6”)
d2.draw(win)
button = Text(Point(175,165),“Roll”)
button.draw(win)
Rectangle(Point(125,140), Point(225,190)).draw(win)
win.getMouse()
try:
dice1 = MSDie(int(d1.getText()))
dice2 = MSDie(int(d2.getText()))
except Exception:
button.setText(“”)
Text(Point(175,155),“Error!!”).draw(win)
Text(Point(175,175),“Try Again”).draw(win)
win.getMouse()
win.close()
main()
dice1.roll()
dice2.roll()
button.setText(“Rolled”)
print “%d %d” %(dice1.getValue(), dice2.getValue())
Text(Point(175, 220), “Sum of 1st dice and 2nd dice”).draw(win)
Text(Point(175, 275), “%d” %(dice1.getValue()+dice2.getValue())).draw(win)
Rectangle(Point(125,250), Point(225,300)).draw(win)
button.setText(“EXIT”)
button.setStyle(‘bold’)
win.getMouse()
win.close()
if __name__ == “__main__”: main()
this code created on python(x,y) with python version 2.7.6
And it’s for the code!
jumlah-2-dadu-gui
Here for screenshot