开发者

Basic python, def function and recall for a text-menu

I've just started playing around with python and was looking for advice.

The problem is with the MENU(), for some reason on the 7th line I get a syntax error

    $ MENU()

Not sure what I'm doing wrong.

def MENU():
    print("Menu:")
    print("     0. Menu")
    print("     1. Random Number Generator")
    access = int(input("Make a selection from the above list: ")

MENU()   ## Problem area

if access == 1:
    ## Random Number Generator
    import random
    ## Imports random functions
    count = 0
    b = 0
    ## Creates the loop function, printing out the dataset
    while count < 100:
        count += 1
        a = random.randrange(1,101)
        print(count,". ", a, end=" | " )
        b += a
    ## Shows the average values for the program, output
    else:
        print()
        print("Finish!")
        print(b)
        print(b/100)
        menu()
else:
    MENU()

Context: I'm using this system just to improve my language as well as prevent myself creating 100's 开发者_如何转开发of 10 line files.


You missed the closing brackets in line 5:

access = int(input("Make a selection from the above list: "))
                                                            ^


I have just had browse through your code, and while you probably have it figured out now I thought a few suggestions might help you get more into python.

Firstly, style is really important with python, being a white space language. The language also has some great features that can condense the amount of code down, which again encourages a good style. There is something called PEP guides, which introduce this. PEP-8 is the style guide for python, I would highly recommend reading through it, if you are getting more into python.

Also when I was learning python I found this learning python the hard way guide an excellent resource. Python is really fun when you get into, hope you're enjoying it! Below is another version of your code that might make more sense.

import random # All imports should be at the top, if you know 
              # you are going to use them.

def menu():
    print("Menu:")
    print("     0. Menu")
    print("     1. Random Number Generator")
    access = int(input("Make a selection from the above list: "))
    return access # See explanation

access = menu() # See explanation

if access == 1:
    count = 0
    b = 0

    while count < 100:
        count += 1
        a = random.randrange(1,101)
        print(count,". ", a, end = " | " )
        b += a

    print()
    print("Finish!")
    print(b)
    print(b/100)
    menu()

else:
    menu()

**Explanation: It is important to store the value of access into a variable here. You cannot set a value inside a function and expect it to update for the rest of the script. This is because of scoping.

**Also if you are expecting the menu to be called again every time after a selection has been executed you while need to rethink the structure somewhat.

Another way of doing this is to use a for loop. To do this you would having something like:

    for i in range(100):
         a = random.randrange(1,101)
         print(count,". ", a, end = " | " )
         b += a

    print()
    print("Finish!")
    print(b)
    print(b/100)
    menu()
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜