我的程序运行良好,但我的客户想要

时间:2014-06-27 23:45:11

标签: python

我的程序运行良好,但我的客户希望我删除point函数括号内的参数,并将确定每个die的语句移动到点。他在说什么?为什么?这将实现什么目标?

现在的代码和输出如下。任何帮助或答案都会很棒。提前谢谢!

import random
import math
MIN = 1
MAX = 6
ROLLS = 5

def main():
    for count in range(ROLLS):
        die_1 = (random.randint(MIN, MAX))
        die_2 = (random.randint(MIN, MAX))
        print()
        print('-----------------------------------------------')
        combined_roll = point(die_1, die_2)
        print('This program simulates two die being rolled five')
        print('times and calculates their total numbers.')
        print('Here are the combined rolls for the dice!')
        print(die_1)
        print(die_2)
        print('The combined roll is:', combined_roll)

def point(die_1, die_2):
    roll_1 = die_1 + die_2

    combined_roll = roll_1 
    return combined_roll
main()

-----------------------------------------------
This program simulates two die being rolled five
times and calculates their total numbers.
Here are the combined rolls for the dice!
4
6
The combined roll is: 10

-----------------------------------------------
This program simulates two die being rolled five
times and calculates their total numbers.
Here are the combined rolls for the dice!
4
5
The combined roll is: 9

-----------------------------------------------
This program simulates two die being rolled five
times and calculates their total numbers.
Here are the combined rolls for the dice!
5
5
The combined roll is: 10

-----------------------------------------------
This program simulates two die being rolled five
times and calculates their total numbers.
Here are the combined rolls for the dice!
2
4
The combined roll is: 6

-----------------------------------------------
This program simulates two die being rolled five
times and calculates their total numbers.
Here are the combined rolls for the dice!
5
1
The combined roll is: 6

2 个答案:

答案 0 :(得分:1)

你问:

  

他在说什么?

def main():
    for count in range(ROLLS):
        print()
        print('-----------------------------------------------')
        combined_roll = point()
        print('This program simulates two die being rolled five')
        print('times and calculates their total numbers.')
        print('Here are the combined rolls for the dice!')
        print('The combined roll is:', combined_roll)

def point():
    die_1 = (random.randint(MIN, MAX))
    die_2 = (random.randint(MIN, MAX))
    print(die_1)
    print(die_2)
    roll_1 = die_1 + die_2
    combined_roll = roll_1 
    return combined_roll
  

为什么呢?这将实现什么目标?

这使得main更加简单,使point()变得更加复杂。

虽然如果你不需要int中的point语句,你可以使print更简单。

def point():
    return (random.randint(MIN, MAX)) + (random.randint(MIN, MAX))

答案 1 :(得分:-1)

你的“客户”(阅读老师)可能只是试图让你模块化它......

def do_roll(number_of_dice):
    points = [random.randint(1,6) for _ in range(number_of_dice)]
    for i,p in enumerate(points,1):
        print "Die %d: %d"%(i,p)
    total = sum(points)
    print "TOTAL:",total
    return total

x = do_roll(2)

但现在如果我想掷3个骰子我可以做

do_roll(3)

或25个骰子

do_roll(25)

你可以更进一步,并指定边数(想想地牢和龙和12边死)

def do_roll(number_of_dice,number_of_sides=6):
    points = [random.randint(1,number_of_sides) for _ in range(number_of_dice)]
    for i,p in enumerate(points,1):
        print "Die %d: %d"%(i,p)
    total = sum(points)
    print "TOTAL:",total
    return total

do_roll(5,12) #roll 5 12 sided dice