如何在凹陷之外带出一个变量?
我创建了一个具有浓度的程序(您知道,例如,循环,如果,elif,else,def以及其他任何在其之后都有凹陷的东西。) 而且这种凹陷中有一些新的创建变量。这样:
.
.
.
little_input = input()
big_input = input()
num = 10
def calculate() :
global num
num += 1
num2 = num * big_input
num3 = num2 / little_input
print(num3)
rmin = num2 % little_input
if rmin == 0 :
print(num3)
.
.
.
然后我看到我需要使用num3变量做点事,但是它需要在def infention之外。,当它在外面时,它说该变量未定义。你能帮我吗?谢谢。
编辑 @nesi:
print("chain wheel calculator")
print("enter the little diameter in mm :")
little_input = input()
print("enter the big diameter in mm :")
big_input = input()
little_input = int(little_input)
big_input = int(big_input)
num = 9
def calculate() :
global num
num += 1
num2 = num * big_input
num3 = num2 / little_input
print(num3)
rmin = num2 % little_input
if rmin == 0 :
print(num3)
while num <= 100 :
calculate()
I created a program that has indention in it (you know , like for and while loops , if , elif , else , def and any another thing that has indention after it.)
and this indention has some new created variables in it. like this :
.
.
.
little_input = input()
big_input = input()
num = 10
def calculate() :
global num
num += 1
num2 = num * big_input
num3 = num2 / little_input
print(num3)
rmin = num2 % little_input
if rmin == 0 :
print(num3)
.
.
.
and then I saw I need to do something with the num3 variable but it needs to be outside of the def indention. and when it's outside , it says the variable is not defined. can you help me with this? thanks.
edit for @Nesi :
print("chain wheel calculator")
print("enter the little diameter in mm :")
little_input = input()
print("enter the big diameter in mm :")
big_input = input()
little_input = int(little_input)
big_input = int(big_input)
num = 9
def calculate() :
global num
num += 1
num2 = num * big_input
num3 = num2 / little_input
print(num3)
rmin = num2 % little_input
if rmin == 0 :
print(num3)
while num <= 100 :
calculate()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该起作用。我不确定您想要什么行为,但我认为您只想返回
num3
如果rmin == 0
。如果rmin!= 0
这将返回无
。因此,语句如果num3:
检查num3不是none
键入,然后打印结果值。您可以在此条件语句中添加您想做的任何您想做的事情。通常,Aviod混合范围像您之前所做的那样。this should work. I am not sure what behaviour you want but I presume you only want to return
num3
ifrmin == 0
. Ifrmin != 0
this will returnNone
. So the statementif num3:
checks if num3 is notNone
type and then prints the resulting value. You can add in whatever you wish to do tonum3
within this conditional statement. Generally, aviod mixing scopes like you were doing before.尝试一下。
Try this.
您将需要在功能之外声明它,然后在功能内部将其用作全局变量。
这里a link 这可能会对您有所帮助。
在这里我得到的示例
输出
You will need to declare it outside the function then inside the function use it as global variable.
Here a link that might help you.
Here an example
Output that i got