我如何在python 3中将多个程序组合在一起?

时间:2017-05-01 01:37:56

标签: python python-3.x

我想要将一些不同的程序合并为一个大程序,但我不知道该怎么做。我的节目是:

import random
Level = int(input('What level is the chest?'))
GP = (random.randint(0, 10))
SP = (random.randint(10, 50))
CP = (random.randint(0, 100))

print ((GP * Level),'GP')
print ((SP * Level),'SP')
print ((CP * Level),'CP')

Number = int(input('How many dice would you like to roll?'))
Sides =  int(input('How many sides?'))
import random
for Number in range(Number):
  print (random.randint(1, Sides))

import random
classes = ["Dwarf","Elf","Halfling","Human","Dragonborn","Gnome","Half-
Elf","Half-Orc","Tiefling"]
r = random.randint(1, 9)
print("Your class is {}".format(classes[r - 1]))

我还想知道如何为此添加新程序。 (是的,这是D& D)

1 个答案:

答案 0 :(得分:0)

如果您想在游戏过程中运行任何这些块,可以使用functionswhile循环来选择要运行的块。也许是这样的:

import random

def get_level():
    Level = int(input('What level is the chest?'))
    GP = (random.randint(0, 10))
    SP = (random.randint(10, 50))
    CP = (random.randint(0, 100))
    print ((GP * Level),'GP')
    print ((SP * Level),'SP')
    print ((CP * Level),'CP')

def get_roll():
    Number = int(input('How many dice would you like to roll?'))
    Sides =  int(input('How many sides?'))
    for Number in range(Number):
        print (random.randint(1, Sides))

def get_class():
    classes = ["Dwarf","Elf","Halfling","Human","Dragonborn","Gnome",
               "Half-Elf","Half-Orc","Tiefling"]
    r = random.randint(1, 9)
    print("Your class is {}".format(classes[r - 1]))

choice = None
while choice != 'quit':
    choice = input('What next? [level, roll, class, quit] ').lower()
    if choice == 'level':
        get_level()
    elif choice == 'roll':
        get_roll()
    elif choice == 'class':
        get_class()
    elif choice == 'quit':
        pass
    else:
        print('Sorry, please try again.')