下面的伪代码提供了一种模拟掷骰子和计算面部数字的算法



下面的伪代码提供了一个模拟掷骰子和计算面部数字的算法。

  1. 启动
  2. initialize done为false
  3. 未完成时
  • 提示用户输入正整数n如果n是正整数设置为true
  • 否则输出有意义的错误消息提示用户再次尝试输入
  1. 初始化一个变量以存储n个整数(例如:将模具滚动30次将存储30个不同的数字(
  2. 循环n次迭代,模具每卷一次
  • 生成一个介于1和6之间的整数
  • 将生成的整数插入/追加到存储变量
  1. 初始化第二个存储变量,以存储1-6之间的每个数字的计数(每个数字滚动了多少次(
  2. 循环n次或六次迭代,具体取决于您选择的对此赋值进行编码的方式
  • 将第一个存储中生成的每个整数(滚动的数字(计数到相应的计数存储
  1. 输出六个计数(例如:1被轧制7次,2被轧制4次,3被轧制6次,等等(
  2. 产量百分比分布(例如:1卷为23.33%,2卷为13.33%,3卷为20.00%,等等(
  3. 停止

我得到了第一部分,但没有得到其余部分。谢谢

#start() function to prompt user for a positive integer n with error checking and meaningful error messages.
done = False
while not done:
#try reading integer input from user
inputNumber = input('Enter a positive integer: ')
try:#if input is positive, output the data.
intInputNumber = int(inputNumber)
if intInputNumber > 0:
done = True
else:#if input is not positive, display error and call this function again
print("Please enter a positive integer. Try again!!")
except ValueError:#if input is not integer, handle error
print('Please enter an integer. Try again!!')

您可以使用python字典像这个

#initialisation
mydictionary={'1':0,'2':0,'3':0,'4':0,'5':0,'6':0}

然后每次你得到一个有效的输入,你就做mydictionary[inputNumber]+=1

最新更新