我正在尝试使用Visual Logic流程图来编写python程序。我无法让它发挥作用。怎么了?请帮忙!对不起,我是一个新手,只是不明白。 这是我到目前为止所做的:
#---------------------------------------------------------------------
# Programmer: Les
# Program name: Program to define an array
# Date written: November 08, 2013
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Introduction lines
print("-"*50 + "\n");
print(("Final Project ---> Rolling a Die") + "\n");
print(("Written by Les") + "\n");
print("-"*50 + "\n");
print(" \n");
#---------------------------------------------------------------------
# Define first array
firstCounters = 1 * 6
#---------------------------------------------------------------------
# Input for first round
firstRollsToMake = int(input("Please enter how many times the die should be rolled in the first round ---> \n "));
print(" \n");
#---------------------------------------------------------------------
# Make die
import random
def dice(sides=6):
return random.randint(1,sides)
#---------------------------------------------------------------------
# Rolling the die
for firstRoll in range(1, firstRollsToMake, 1):
dieValue = float(random.randint(min,max));
firstCounters[dieValue] = (firstCounters[dieValue] + 1);
# End for loop
print();
print("-"*50 + "\n");
print(" \n");
#---------------------------------------------------------------------
# Display the percentages
print(format("The value ") + format(firstRollStats) + format(" was rolled ") + format(firstCounters(firstRollStats)) + format(" times") + format(" \n"));
diePercentageOne = (firstCounters(firstRollStats))/firstRollsToMake
print(format(" or ") + format(FormatPercent(diePercentageOne)) + format(" \n"));
#---------------------------------------------------------------------
# Display histogram results
print(format("Below are the results in histogram form\n"));
for histogram in range(1, 6, 1):
print(format(histogram) + (" : "));
for circleLoop in range(1, firstCounters[histogram], 1):
print(format("0"));
print(format(" \n"));
print(format(" \n"));
#---------------------------------------------------------------------
# Define second array
secondCounters = 1 * 6
#---------------------------------------------------------------------
# Input for second round
secondRollsToMake = int(input("Please enter how many times the die should be rolled in the second round ---> \n "));
print(" \n");
#---------------------------------------------------------------------
# Rolling the die again
for secondRoll in range(1, secondRollsToMake, 1):
dieValueTwo = float(random.randint(min,max));
secondCounters[dieValueTwo] = (secondCounters[dieValueTwo] + 1);
# End for loop
print();
print("-"*50 + "\n");
print(" \n");
#---------------------------------------------------------------------
# Display the second percentages
print(format("The value ") + format(secondRollStats) + format(" was rolled ") + format(secondCounters(secondRollStats)) + format(" times") + format(" \n"));
diePercentageTwo = (secondCounters(secondRollStats))/secondRollsToMake
print(format(" or ") + format(FormatPercent(diePercentageTwo)) + format(" \n"));
#---------------------------------------------------------------------
# Display second histogram results
print(format("Below are the results in histogram form\n"));
for histogramTwo in range(1, 6, 1):
print(format(histogramTwo) + (" : "));
for circleLoop in range(1, secondCounters[histogramTwo], 1):
print(format("0"));
print(format(" \n"));
print(format(" \n"));
#---------------------------------------------------------------------
# Number of times rolled comment
print(format("Comment on the number of times rolled:\n"));
print(format("\n"));
while (firstRollsToMake == secondRollsToMake):
if (firstRollsToMake == secondRollsToMake):
print(format("Since the die was rolled the same amount of times in both rounds, the results are not skewed and can be compared and contrasted fairly\n"));
elif (firstRollsToMake < secondRollsToMake):
print(format("Since the die was rolled fewer times in the first round than the second, the results are skewed and cannot be compared and contrasted fairly\n"));
else:
print(format("Since the die was rolled more times in the first round than the second, the results are skewed and cannot be compared and contrasted fairly\n"));
#---------------------------------------------------------------------
# End program
答案 0 :(得分:2)
我已经回答了你的两个朋友,他们的任务相同:
import random
from collections import defaultdict
def main():
dice = int(input("Enter the number of dice: "))
sides = int(input("Enter the number of sides: "))
rolls = int(input("Enter the number of rolls to simulate: "))
result = roll(dice, sides, rolls)
maxH = 0
for i in range(dice, dice * sides + 1):
if result[i] / rolls > maxH: maxH = result[i] / rolls
for i in range(dice, dice * sides + 1):
print('{:2d}{:10d}{:8.2%} {}'.format(i, result[i], result[i] / rolls, '#' * int(result[i] / rolls / maxH * 40)))
def roll(dice, sides, rolls):
d = defaultdict(int)
for _ in range(rolls):
d[sum(random.randint(1, sides) for _ in range(dice))] += 1
return d
main()
您可以取出对您有用的部分。这还应该包括后续问题,例如:“我如何整齐地打印”和“如何绘制直方图”。
示例:
Enter the number of dice: 2
Enter the number of sides: 6
Enter the number of rolls to simulate: 1000
2 28 2.80% ######
3 59 5.90% #############
4 84 8.40% ###################
5 96 9.60% ######################
6 155 15.50% ####################################
7 170 17.00% ########################################
8 147 14.70% ##################################
9 102 10.20% #######################
10 80 8.00% ##################
11 50 5.00% ###########
12 29 2.90% ######