【发布时间】:2015-09-04 19:28:18
【问题描述】:
我以为我终于解决了我为练习而编写的掷骰子代码中的所有问题,但显然我昨晚忘记保存我的工作,当我今天下午重写时,random.randint 函数没有' 似乎没有返回除 1 和 2 以外的任何数字,在我记得之前,当给定更大尺寸的骰子时,它会返回更高的数字。我在 shell 中单独运行了滚动功能,它似乎工作正常,但我认为我的整体代码一定有问题,导致它将每次滚动都视为 1d2。
import random
die = 0
num = 0
def Opening():
print ("Welcome to the Super Deluxe Diceroller mk. 3!")
print ("What sort of Dice would you like to roll")
choice()
def choice():
print ("A. d2")
print ("B. d4")
print ("C. d6")
print ("D. d8")
print ("E. d10")
print ("F. d12")
print ("G. d20")
print ("H. d100")
global sides
die = input()
if die == 'a' or 'A':
sides = 2
nombre()
elif die == 'b' or 'B':
sides = 4
nombre()
elif die == 'c' or 'C':
sides = 6
nombre()
elif die == 'd' or 'D':
sides = 8
nombre()
elif die == 'e' or 'E':
sides = 10
nombre()
elif die == 'f' or 'F':
sides = 12
nombre()
elif die == 'g' or 'G':
sides = 20
nombre()
elif die == 'h' or 'H':
sides = 100
nombre()
else:
return 'Invalid'
choice()
def nombre():
print ("How many dice would you like to roll?")
global num
num = input()
if num.isnumeric():
roll()
else:
return 'Invalid'
nombre()
def roll():
global num
global fortuna
global sides
if int(num) > 0:
fortuna = random.randint(1,sides)
num = int(num)
num = num - 1
print(fortuna)
roll()
else:
retry()
def retry():
print("Would you like to roll again? (y/n)")
ans = input()
if ans == 'y':
Opening()
elif ans == 'n':
exit
else:
return 'Invalid'
retry()
Opening()
【问题讨论】:
标签: function python-3.x random dice