基本的 python、def 函数和文本菜单的调用
我刚刚开始玩 python,并正在寻求建议。
问题出在 MENU() 上,由于某种原因,在第 7 行我收到语法错误,
$ MENU()
不确定我做错了什么。
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()
背景:我使用这个系统只是为了提高我的语言以及防止自己创建 100 个 10 行文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您错过了第 5 行的右括号:
You missed the closing brackets in line 5:
我刚刚浏览了你的代码,虽然你现在可能已经明白了,但我认为一些建议可能会帮助你更多地了解 python。
首先,对于 Python 来说,风格非常重要,因为它是一种空白语言。该语言还有一些很棒的功能,可以压缩代码量,这再次鼓励了良好的风格。有一个叫做 PEP 指南的东西介绍了这一点。 PEP-8 是 python 的风格指南,我强烈建议阅读如果你对Python有更多的了解的话。
另外,当我学习Python时,我发现这个学习Python的困难方法指南是一个很好的资源。当你接触Python时,它真的很有趣,希望你喜欢它!下面是您的代码的另一个版本,可能更有意义。
**说明:将访问值存储到此处的变量中非常重要。您不能在函数内设置值并期望它针对脚本的其余部分进行更新。这是因为范围界定。
**此外,如果您希望每次执行选择后再次调用菜单,则需要稍微重新考虑结构。
另一种方法是使用 for 循环。要做到这一点,你会得到类似的东西:
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.
**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: