这个问题似乎很容易回答,只要为我在这个范围内的最大值做一个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()
我有随机导入,但这只是整个程序的一部分。如果没有捷径的话,我能理解,但是我想在把它全部打出来之前检查一下,因为可能没有必要。你知道吗
第一次改变
inphandle
然后改变
我会这么做:
将inphandle更改为:
将main()改为:
只是澄清一下random.randrange随机范围地址:
from是最小值,to是最大值,除了它本身。 所以如果你想要一个1到4的范围,就把1到5。 增量:输入1表示“1”的增量,因此可能的输出为1、2、3或4,而不是1.3252342或2.446211
我建议创建一个处理不同类型骰子的类:
我想说,这是一个有效的情况下,你可以学习的类;)
然后使用类:
请注意,我将
min
&;max
计数器移到了main,因为另一种方法是生成不好的globals。你知道吗我还重命名了它们,因为
min
和max
是内置函数的名称。最好学着不要把这些藏起来。你知道吗相关问题 更多 >
编程相关推荐