Python variables passing
I want to create a set_range procedures whose goal is to define biggest and smallest number from group of 3. Final step would be minus operation.
I wrote the first two parts but final part is not working. The issue seems to be with passing the variables from one to another...
#biggest number
def biggest(a, y, z):
Max = a
if y > Max:
Max = y
if z > Max:
Max = z
if y > z:
Max = y
return Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(a, y, z):
Small = a
if y < Small:
Small = y
if z < Small:
Small = z
if y < z:
Small = y
return Small
#print smallest (10, 4, 7) TEST ONLY
#final part of the code, Max - Small operation
def set_range():
m = Max
s = Small
print set_range
python variables optimization procedure
|
show 3 more comments
I want to create a set_range procedures whose goal is to define biggest and smallest number from group of 3. Final step would be minus operation.
I wrote the first two parts but final part is not working. The issue seems to be with passing the variables from one to another...
#biggest number
def biggest(a, y, z):
Max = a
if y > Max:
Max = y
if z > Max:
Max = z
if y > z:
Max = y
return Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(a, y, z):
Small = a
if y < Small:
Small = y
if z < Small:
Small = z
if y < z:
Small = y
return Small
#print smallest (10, 4, 7) TEST ONLY
#final part of the code, Max - Small operation
def set_range():
m = Max
s = Small
print set_range
python variables optimization procedure
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
2
Just to be clear: You're aware thatmax
andmin
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer isreturn max(a, y, z)
andreturn min(a, y, z)
.
– ShadowRanger
Oct 5 '17 at 22:00
3
You need to actually callbiggest
andsmallest
functions insideset_range
.set_range
will also need to accept the three numbers to be compared.
– kindall
Oct 5 '17 at 22:01
3
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
1
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02
|
show 3 more comments
I want to create a set_range procedures whose goal is to define biggest and smallest number from group of 3. Final step would be minus operation.
I wrote the first two parts but final part is not working. The issue seems to be with passing the variables from one to another...
#biggest number
def biggest(a, y, z):
Max = a
if y > Max:
Max = y
if z > Max:
Max = z
if y > z:
Max = y
return Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(a, y, z):
Small = a
if y < Small:
Small = y
if z < Small:
Small = z
if y < z:
Small = y
return Small
#print smallest (10, 4, 7) TEST ONLY
#final part of the code, Max - Small operation
def set_range():
m = Max
s = Small
print set_range
python variables optimization procedure
I want to create a set_range procedures whose goal is to define biggest and smallest number from group of 3. Final step would be minus operation.
I wrote the first two parts but final part is not working. The issue seems to be with passing the variables from one to another...
#biggest number
def biggest(a, y, z):
Max = a
if y > Max:
Max = y
if z > Max:
Max = z
if y > z:
Max = y
return Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(a, y, z):
Small = a
if y < Small:
Small = y
if z < Small:
Small = z
if y < z:
Small = y
return Small
#print smallest (10, 4, 7) TEST ONLY
#final part of the code, Max - Small operation
def set_range():
m = Max
s = Small
print set_range
python variables optimization procedure
python variables optimization procedure
edited Oct 5 '17 at 22:25
user2357112
157k13174267
157k13174267
asked Oct 5 '17 at 21:57
marcin2x4marcin2x4
196
196
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
2
Just to be clear: You're aware thatmax
andmin
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer isreturn max(a, y, z)
andreturn min(a, y, z)
.
– ShadowRanger
Oct 5 '17 at 22:00
3
You need to actually callbiggest
andsmallest
functions insideset_range
.set_range
will also need to accept the three numbers to be compared.
– kindall
Oct 5 '17 at 22:01
3
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
1
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02
|
show 3 more comments
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
2
Just to be clear: You're aware thatmax
andmin
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer isreturn max(a, y, z)
andreturn min(a, y, z)
.
– ShadowRanger
Oct 5 '17 at 22:00
3
You need to actually callbiggest
andsmallest
functions insideset_range
.set_range
will also need to accept the three numbers to be compared.
– kindall
Oct 5 '17 at 22:01
3
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
1
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
2
2
Just to be clear: You're aware that
max
and min
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer is return max(a, y, z)
and return min(a, y, z)
.– ShadowRanger
Oct 5 '17 at 22:00
Just to be clear: You're aware that
max
and min
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer is return max(a, y, z)
and return min(a, y, z)
.– ShadowRanger
Oct 5 '17 at 22:00
3
3
You need to actually call
biggest
and smallest
functions inside set_range
. set_range
will also need to accept the three numbers to be compared.– kindall
Oct 5 '17 at 22:01
You need to actually call
biggest
and smallest
functions inside set_range
. set_range
will also need to accept the three numbers to be compared.– kindall
Oct 5 '17 at 22:01
3
3
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
1
1
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02
|
show 3 more comments
1 Answer
1
active
oldest
votes
This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()
class getOperationMax(a,y,z):
def __init__(self,a,y,z):
self.y = y
self.a = y
self.z = y
self.Max = 0
self.Small = 0
#biggest number
def biggest(self):
self.Max = self.a
if self.y > self.Max :
self.Max = self.y
self.max = self.y
if self.z > self.Max:
self.Max = self.z
if self.y > self.z:
self.Max = self.y
return self.Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(self):
self.Small = self.a
if self.y < self.Small:
self.Small = self.y
if self.z < self.Small:
self.Small = self.z
if self.y < self.z:
self.Small = self.y
return self.Small
#print smallest (10, 4, 7) TEST ONLY
operation = getOperationMax(5,6,7)
print operation.biggest()
print operation.smallest()
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46595398%2fpython-variables-passing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()
class getOperationMax(a,y,z):
def __init__(self,a,y,z):
self.y = y
self.a = y
self.z = y
self.Max = 0
self.Small = 0
#biggest number
def biggest(self):
self.Max = self.a
if self.y > self.Max :
self.Max = self.y
self.max = self.y
if self.z > self.Max:
self.Max = self.z
if self.y > self.z:
self.Max = self.y
return self.Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(self):
self.Small = self.a
if self.y < self.Small:
self.Small = self.y
if self.z < self.Small:
self.Small = self.z
if self.y < self.z:
self.Small = self.y
return self.Small
#print smallest (10, 4, 7) TEST ONLY
operation = getOperationMax(5,6,7)
print operation.biggest()
print operation.smallest()
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
add a comment |
This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()
class getOperationMax(a,y,z):
def __init__(self,a,y,z):
self.y = y
self.a = y
self.z = y
self.Max = 0
self.Small = 0
#biggest number
def biggest(self):
self.Max = self.a
if self.y > self.Max :
self.Max = self.y
self.max = self.y
if self.z > self.Max:
self.Max = self.z
if self.y > self.z:
self.Max = self.y
return self.Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(self):
self.Small = self.a
if self.y < self.Small:
self.Small = self.y
if self.z < self.Small:
self.Small = self.z
if self.y < self.z:
self.Small = self.y
return self.Small
#print smallest (10, 4, 7) TEST ONLY
operation = getOperationMax(5,6,7)
print operation.biggest()
print operation.smallest()
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
add a comment |
This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()
class getOperationMax(a,y,z):
def __init__(self,a,y,z):
self.y = y
self.a = y
self.z = y
self.Max = 0
self.Small = 0
#biggest number
def biggest(self):
self.Max = self.a
if self.y > self.Max :
self.Max = self.y
self.max = self.y
if self.z > self.Max:
self.Max = self.z
if self.y > self.z:
self.Max = self.y
return self.Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(self):
self.Small = self.a
if self.y < self.Small:
self.Small = self.y
if self.z < self.Small:
self.Small = self.z
if self.y < self.z:
self.Small = self.y
return self.Small
#print smallest (10, 4, 7) TEST ONLY
operation = getOperationMax(5,6,7)
print operation.biggest()
print operation.smallest()
This way you can access all variables from all methods within your code this means, share numbers from biggest() and smallest()
class getOperationMax(a,y,z):
def __init__(self,a,y,z):
self.y = y
self.a = y
self.z = y
self.Max = 0
self.Small = 0
#biggest number
def biggest(self):
self.Max = self.a
if self.y > self.Max :
self.Max = self.y
self.max = self.y
if self.z > self.Max:
self.Max = self.z
if self.y > self.z:
self.Max = self.y
return self.Max
#print biggest(10, 4, 7) TEST ONLY
#smallest number
def smallest(self):
self.Small = self.a
if self.y < self.Small:
self.Small = self.y
if self.z < self.Small:
self.Small = self.z
if self.y < self.z:
self.Small = self.y
return self.Small
#print smallest (10, 4, 7) TEST ONLY
operation = getOperationMax(5,6,7)
print operation.biggest()
print operation.smallest()
edited Oct 5 '17 at 22:20
answered Oct 5 '17 at 22:07
m1009ct0m1009ct0
1,53611639
1,53611639
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
add a comment |
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
Would you mind explaining your code?
– Right leg
Oct 5 '17 at 22:09
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f46595398%2fpython-variables-passing%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
create a global variable.
– m1009ct0
Oct 5 '17 at 21:59
2
Just to be clear: You're aware that
max
andmin
already handle this, right? If it's a class assignment, do what you need to do, but otherwise, the answer isreturn max(a, y, z)
andreturn min(a, y, z)
.– ShadowRanger
Oct 5 '17 at 22:00
3
You need to actually call
biggest
andsmallest
functions insideset_range
.set_range
will also need to accept the three numbers to be compared.– kindall
Oct 5 '17 at 22:01
3
@eddwinpaz: What persistent state is needed to compute min and max? Forcing classes on something is pointless when there is no state to maintain, nor behaviors of said state.
– ShadowRanger
Oct 5 '17 at 22:01
1
@eddwinpaz neither of those approaches is necessary, really. You can just use normal functions and pass parameters between them. Indeed, I think suggesting using global variables is bad advice. As for your class suggesting, that is one approach, albeit overkill in this case, IMO, but regardless python doesn't have access modifiers so there are no "public" and "private" methods
– juanpa.arrivillaga
Oct 5 '17 at 22:02