如何检测何时滚动到最大数量?

2024-04-27 03:11:19 发布

您现在位置:Python中文网/ 问答频道 /正文

这个问题似乎很容易回答,只要为我在这个范围内的最大值做一个if语句,但请花一点时间阅读,然后再叫我白痴。我正在做一个程序,让用户选择许多不同的骰子。我想知道是否有一种简单的方法来检测何时从任何选项中滚动最大值,而不设置所有不同骰子的if语句。你知道吗

def inphandle(choice):
    if choice==4:
        roll = random.randint(1,4)
    elif choice==6:
        roll = random.randint(1,6)
    elif choice==8:
        roll = random.randint(1,8)
    elif choice==10:
        roll = random.randint(1,10)
    elif choice==20:
        roll = random.randint(1,20)
    return roll

def dice(roll):
    min = 0
    if roll==1:
        print("Min roll! Try again!")
        min = min+1
    if roll 


def mainmenu():
    print("Please choose from the following options:")
    print("Roll | EXIT")
    option = input()
    if option=="EXIT" or option=="exit" or option=="Exit"
        print("You rolled" + max + "max rolls and " + min + " min rolls! Goodbye!")

def main():
    choice = int(input("Please enter a number corresponding to the number of faces on your dice. E.x. 4 for 4-Sided: "))
    cont = input("Would you like to roll the dice? Y or N: ")
    while cont=="y" or cont=="Y":
        roll = inphandle(choice)
        dice(roll)
        cont = input("Would you like to roll again? Y or N: ")
    while cont=="n" or cont=="N":
        easteregg()
        cont = input("Do I get bunus points now?!?")
main()

我有随机导入,但这只是整个程序的一部分。如果没有捷径的话,我能理解,但是我想在把它全部打出来之前检查一下,因为可能没有必要。你知道吗


Tags: ortheinputifdefrandommindice
3条回答

第一次改变inphandle

def inphandle(choice):
    return random.randint(1,choice)

然后改变

    while cont=="y" or cont=="Y":
        roll = inphandle(choice)
        dice(roll,choice)

   ...
 def dice(roll,max_val):
       if roll == max_val:print "MAX"
       elif roll == 1:print "MIN"

我会这么做:

将inphandle更改为:

def inphandle(choice):
  roll = random.randrange(1,choice+1,1)
  return roll

将main()改为:

def main():
  choice = int(input("Please enter a number corresponding to the number of faces on your dice. E.x. 4 for 4-Sided: "))
  valid_inputs = [4,6,8,10,12]
  if choice not in valid_inputs:
    print('Valid inputs are: 4,6,8,10,12. Try again.')
    main()
  cont = input("Would you like to roll the dice? Y or N: ")
  while cont=="y" or cont=="Y":
    roll = inphandle(choice)
    dice(roll)
    cont = input("Would you like to roll again? Y or N: ")
  while cont=="n" or cont=="N":
    easteregg()
    cont = input("Do I get bunus points now?!?")

只是澄清一下random.randrange随机范围地址:

random,randrange('from','to','increments')

from是最小值,to是最大值,除了它本身。 所以如果你想要一个1到4的范围,就把1到5。 增量:输入1表示“1”的增量,因此可能的输出为1、2、3或4,而不是1.3252342或2.446211

我建议创建一个处理不同类型骰子的类:

class Die(object):
    def __init__(self, num_faces):
        self.num_faces = num_faces

    def roll(self):
        return random.randint(1, self.num_faces)

    def is_min(self, number):
        return number == 1

    def is_max(self, number):
        return number == self.num_faces

我想说,这是一个有效的情况下,你可以学习的类;)

然后使用类:

def main():
    minimums = maximums = 0
    choice = int(input("Please enter a number corresponding to the"
                       " number of faces on your dice. E.x. 4 for 4-Sided: "))
    die = Die(choice)
    cont = input("Would you like to roll the dice? Y or N: ")
    while cont.lower() == "y":
        number = die.roll()
        if die.is_min(number):
            minimums += 1
        if die.is_max(number):
            maximums += 1
        cont = input("Would you like to roll again? Y or N: ")

main()

请注意,我将min&;max计数器移到了main,因为另一种方法是生成不好的globals。你知道吗

我还重命名了它们,因为minmax是内置函数的名称。最好学着不要把这些藏起来。你知道吗

相关问题 更多 >