Re: Python homework
import random def rollDie(num): sides = {'One':0, 'Two':0,'Three':0,'Four':0,'Five':0,'Six':0} for i in range(num): rolls = int(random.randint(1, 6)) if rolls == 1: sides['One'] += 1 if rolls == 2: sides['Two'] += 1 if rolls == 3: sides['Three'] += 1 if rolls == 4: sides['Four'] += 1 if rolls == 5: sides['Five'] += 1 if rolls == 6: sides['Six'] += 1 return sides,max(sides,key=sides.get) print(rollDie(50)) *** OUTPUT * ({'One': 10, 'Two': 7, 'Three': 7, 'Four': 11, 'Five': 7, 'Six': 8}, 'Four') -- https://mail.python.org/mailman/listinfo/python-list
Re: Round to 2 decimal places
import math print("This program will calculate the surface area and volume of a 3-dimensional cone: ") print() print() r = input("What is the radius in feet? (no negatives): ") h = input("What is the height in feet? (no negatives): ") r = float(r) h = float(h) if r > 0 and h > 0: surfacearea = math.pi * r ** 2 + r * math.pi * (math.sqrt(r ** 2 + h ** 2)) volume = (1 / 3) * math.pi * r ** 2 * h print() print("Your Answer is:") print() print("A cone with radius", r, "\nand height of", h, "\nhas a volume of : ", round(volume,2), "\nand surface area of : ", round(surfacearea,2), ) else: print("No negatives allowed, try again.") -- https://mail.python.org/mailman/listinfo/python-list
Re: Round to 2 decimal places
The following works: import math print("This program will calculate the surface area and volume of a 3-dimensional cone: ") print() print() r = input("What is the radius in feet? (no negatives): ") h = input("What is the height in feet? (no negatives): ") r = float(r) h = float(h) if r > 0 and h > 0: surfacearea = math.pi * r ** 2 + r * math.pi * (math.sqrt(r ** 2 + h ** 2)) volume = (1 / 3) * math.pi * r ** 2 * h print() print("Your Answer is:") print() print("A cone with radius", r, "\nand height of", h, "\nhas a volume of : ", round(volume,2), "\nand surface area of : ", round(surfacearea,2), ) else: print("No negatives allowed, try again.") -- https://mail.python.org/mailman/listinfo/python-list
Re: Python homework
On Wednesday, December 6, 2017 at 9:28:26 PM UTC+11, D'Arcy Cain wrote: > On 12/05/2017 07:33 PM, nick.martinez2--- via Python-list wrote: > > I have a question on my homework. My homework is to write a program in > > which the computer simulates the rolling of a die 50 > > times and then prints > > (i). the most frequent side of the die > > (ii). the average die value of all rolls. > > I wrote the program so it says the most frequent number out of all the > > rolls for example (12,4,6,14,10,4) and will print out "14" instead of 4 > > like I need. > > How did you come up with 4? I come up with 3.36 with those numbers. > > > This is what I have so far: > > import random > > > > def rollDie(number): > > rolls = [0] * 6 > > For a small efficiency use "[0] * 7". See below for reason. > > > for i in range(0, number): > > roll=int(random.randint(1,6)) > > rolls[roll - 1] += 1 > > Use "rolls[roll] += 1" here. You save one arithmetic instruction each > time through the loop. Fifty times isn't much saving but imagine fifty > million. > > > return rolls > > return rolls[1:]. No matter how many rolls you only need to do the > slice once. However, you really need a lot of iterations before it > really affects the total run time. > > You need one more thing though. Create a new variable called "total" > and set it to 0.0. Add "total += roll" to your loop. your return > statement is now "return rolls[1:], total/number". > > > > > if __name__ == "__main__": > > result = rollDie(50) > > Now "result, average = ..." > > > print (result) > > print(max(result)) > > This is why you get 14. The maximum number of rolls for any one side is > 14 for side 4. Is that where you got "4"? > > -- > D'Arcy J.M. Cain > Vybe Networks Inc. > http://www.VybeNetworks.com/ > IM:da...@vex.net VoIP: sip:da...@vybenetworks.com The 'four' is the number which was rolled the highest during the 50 rolls -- https://mail.python.org/mailman/listinfo/python-list
Re: Round to 2 decimal places
On Thursday, December 7, 2017 at 12:39:38 PM UTC+11, nick martinez wrote: > On Wednesday, December 6, 2017 at 8:13:36 PM UTC-5, ssghot...@gmail.com wrote: > > The following works: > > > > import math > > > > print("This program will calculate the surface area and volume of a > > 3-dimensional cone: ") > > print() > > print() > > r = input("What is the radius in feet? (no negatives): ") > > h = input("What is the height in feet? (no negatives): ") > > r = float(r) > > h = float(h) > > if r > 0 and h > 0: > > > > surfacearea = math.pi * r ** 2 + r * math.pi * (math.sqrt(r ** 2 + h ** > > 2)) > > volume = (1 / 3) * math.pi * r ** 2 * h > > > > print() > > print("Your Answer is:") > > print() > > > > print("A cone with radius", r, "\nand height of", h, "\nhas a volume of > > : ", round(volume,2), "\nand surface area of : ", > > round(surfacearea,2), ) > > else: > > print("No negatives allowed, try again.") > > Tried this but it doesn't seem to work. It still prints out all of the > decimals This is the output from the code above (Working): What is the radius in feet? (no negatives): >? 2 What is the height in feet? (no negatives): >? 4 Your Answer is: A cone with radius 2.0 and height of 4.0 has a volume of : 16.76 and surface area of : 40.67 -- https://mail.python.org/mailman/listinfo/python-list
Re: Python homework
import random def rollDie(num): sides = {'One':0, 'Two':0,'Three':0,'Four':0,'Five':0,'Six':0} for i in range(num): rolls = int(random.randint(1, 6)) if rolls == 1: sides['One'] += 1 if rolls == 2: sides['Two'] += 1 if rolls == 3: sides['Three'] += 1 if rolls == 4: sides['Four'] += 1 if rolls == 5: sides['Five'] += 1 if rolls == 6: sides['Six'] += 1 return sides,max(sides,key=sides.get) print(rollDie(50)) *** OUTPUT * ({'One': 10, 'Two': 7, 'Three': 7, 'Four': 11, 'Five': 7, 'Six': 8}, 'Four') -- https://mail.python.org/mailman/listinfo/python-list