开发者

How do I have a program reference an internal random result and apply it to a list to print a result?

Ok, I need some help here. I'm attempting to write a program that rolls a die internally (randrange), which is referenced to a list that will then print the result onto the screen

Example: User inputs "1", activating the dice roll. The program rolls a 2 and checks a list to see what 2 means. 2 means Medium, so it then prints "Medium"

However, I can't figure out where I'd look for a tutorial on this (as I can't figure out what I'd even search for), and everyone I ask gets confused by the question. So I come to you with a pasted set of code. Here's a pastebin for better viewing: http://pastebin.com/PGEmNqTm

import random

def Main() :

    print TESTING
    print
    print
    print "1 1d4"

    sum = raw_input("> ")

    if sum == '1':
        numberr = random.randrange(1, 5)
            if numberr = 1
                print "Small"
            elif numberr = 2
          开发者_运维知识库      print "Medium"
            elif numberr = 3
                print "Large"
            elif numberr = 4
                print "Huge"

while 1:
    input = raw_input("Press Enter to continue or q to quit").upper()
    if input == 'Q': break
    elif input == '' : Main()


May be you can leverage from this example:

import random

def roll(num):
    return  {
        1: "Small",
        2: "Medium",
        3: "Large",
        4: "Huge",
        5: "Huge+",
        6: "Huge++"
    }[num]

print roll(random.randrange(1, 7))


Aside from a few small details, your code seems pretty good as it is.

Those are:

  • Your if numberr == x: lines shouldn't be indented because they're not part of a new block.

  • You need to double the = sign when doing a test: if number == 1:

  • You need a colon for an if (which you knew, but it was missing on some lines).

  • raw_input not raw_imput

  • Quotes around "TESTING"

Here's the code with those fixes:

import random

def Main() :

    print "TESTING"
    print
    print
    print "1 1d4"

    sum = raw_input("> ")

    if sum == '1':
        numberr = random.randrange(1, 5)
        if numberr == 1:
            print "Small"
        elif numberr == 2:
            print "Medium"
        elif numberr == 3:
            print "Large"
        elif numberr == 4:
            print "Huge"

while 1:
    input = raw_input("Press Enter to continue or q to quit").upper()
    if input == 'Q': break
    elif input == '' : Main()

So I'd say you already seem to have it right.

(Also Vishal's advice is good too)


Try something like this:

import random

myNum = ({1:'Small', 2:'Medium', 3:'Large', 4:'Huge'})

def main():
    mysum = raw_input("> ")
    if mysum == '1':
        numberr = random.randrange(1,5)
        print(myNum[numberr])

while 1:
    myinput = raw_input("Press Enter to continue or q to quit")
    if myinput == 'Q':
        break
    elif myinput == '' :
        main()

Be careful with naming your variable names reserved keywords!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜