根据用户需要重复算法次数(PYTHON 3.3.2 GUI)

时间:2014-03-10 21:37:02

标签: python algorithm python-3.x repeat

我正在尝试重复此算法并使用我的代码中的值,请帮助!!!

导入随机

def dice_roll(number):
    if number == 12:
        number = random.randint(1,12)
        print(number)
        return(number)
    elif number == 6:
         number = random.randint(1,12)
         print(number)
         return(number)
    else:
        number == 4
        number = random.randint(1,12)
        print(number)
        return(number)

dice_roll(int(输入(“你要掷哪个骰子? - >”)))

2 个答案:

答案 0 :(得分:0)

执行此操作的典型方法是while True循环,用户退出时break,例如:

while True:
    user_input = int(input("Which die would you like to roll (0 to quit)? "))
    if user_input in (4, 6, 12):
        dice_roll(user_input)
    elif user_input:
        print("Not an acceptable die.")
    else:
        break

请注意,您的dice_roll功能目前始终使用12面模具,并且可以简化:

def dice_roll(sides):
    number = random.randint(1, sides)
    print(number)
    return number

(这也将限制仅移除到4,6和12,因此您可能需要相应地修改输入循环。)

答案 1 :(得分:0)

是这样的吗?

import random

def dice_roll(number):
    if number == 12:
        number = random.randint(1,12)
        print(number)
        return(number)
    elif number == 6:
        number = random.randint(1,12)
        print(number)
        return(number)
    else:
        number == 4
        number = random.randint(1,12)
        print(number)
        return(number)

while True:
   dice_roll(int(input("Which dice would you like to roll? --> ")))
   doRepeat=input('Go again?')
   if doRepeat == 'no':
      break

编辑:

 raw_input() was renamed to input().

你可以在3.x中使用“input()”,就像旧版本一样:

doRepeat=eval(input('Go again?'))