How do I make my variable have a default value “0” when no input has been provided by the user in python?
Here is my code. I want my variable "a" to hold a default value 0 whenever the user has not given any particular value. I have given my code below:
from tkinter import *
from tkinter import messagebox
def sample():
a= show_values1()
v= int(a)+10
print (v)
def show_values1(event=None):
global a
a= i1.get()
print(a)
if int(a) > ul or int(a) < ll :
print('Limit Exceed')
messagebox.showerror("Error", "Please enter values from -100 to 100")
else :
return a
root= Tk()
ul= 100
ll=-100
i1 = inputBox = Entry()
i1.place(x=70, y=48, height=20)
bu1=Button(text='Enter', command = show_values1)
bu1.place(x = 70, y = 28, width=40, height=20)
bu2=Button(text='SEND', command = sample)
bu2.bind('<Return>', lambda x: show_values1(event=None))
bu2.place(x = 70, y = 98, width=40, height=20)
root.mainloop()
python tkinter
add a comment |
Here is my code. I want my variable "a" to hold a default value 0 whenever the user has not given any particular value. I have given my code below:
from tkinter import *
from tkinter import messagebox
def sample():
a= show_values1()
v= int(a)+10
print (v)
def show_values1(event=None):
global a
a= i1.get()
print(a)
if int(a) > ul or int(a) < ll :
print('Limit Exceed')
messagebox.showerror("Error", "Please enter values from -100 to 100")
else :
return a
root= Tk()
ul= 100
ll=-100
i1 = inputBox = Entry()
i1.place(x=70, y=48, height=20)
bu1=Button(text='Enter', command = show_values1)
bu1.place(x = 70, y = 28, width=40, height=20)
bu2=Button(text='SEND', command = sample)
bu2.bind('<Return>', lambda x: show_values1(event=None))
bu2.place(x = 70, y = 98, width=40, height=20)
root.mainloop()
python tkinter
if not i1.get(): a = "0"
– Filip Młynarski
Nov 25 '18 at 16:19
2
You seem to be familiar withif/elsestatements - what's holding you back?
– timgeb
Nov 25 '18 at 16:21
add a comment |
Here is my code. I want my variable "a" to hold a default value 0 whenever the user has not given any particular value. I have given my code below:
from tkinter import *
from tkinter import messagebox
def sample():
a= show_values1()
v= int(a)+10
print (v)
def show_values1(event=None):
global a
a= i1.get()
print(a)
if int(a) > ul or int(a) < ll :
print('Limit Exceed')
messagebox.showerror("Error", "Please enter values from -100 to 100")
else :
return a
root= Tk()
ul= 100
ll=-100
i1 = inputBox = Entry()
i1.place(x=70, y=48, height=20)
bu1=Button(text='Enter', command = show_values1)
bu1.place(x = 70, y = 28, width=40, height=20)
bu2=Button(text='SEND', command = sample)
bu2.bind('<Return>', lambda x: show_values1(event=None))
bu2.place(x = 70, y = 98, width=40, height=20)
root.mainloop()
python tkinter
Here is my code. I want my variable "a" to hold a default value 0 whenever the user has not given any particular value. I have given my code below:
from tkinter import *
from tkinter import messagebox
def sample():
a= show_values1()
v= int(a)+10
print (v)
def show_values1(event=None):
global a
a= i1.get()
print(a)
if int(a) > ul or int(a) < ll :
print('Limit Exceed')
messagebox.showerror("Error", "Please enter values from -100 to 100")
else :
return a
root= Tk()
ul= 100
ll=-100
i1 = inputBox = Entry()
i1.place(x=70, y=48, height=20)
bu1=Button(text='Enter', command = show_values1)
bu1.place(x = 70, y = 28, width=40, height=20)
bu2=Button(text='SEND', command = sample)
bu2.bind('<Return>', lambda x: show_values1(event=None))
bu2.place(x = 70, y = 98, width=40, height=20)
root.mainloop()
python tkinter
python tkinter
asked Nov 25 '18 at 16:16
Vishvachi SinhaVishvachi Sinha
2217
2217
if not i1.get(): a = "0"
– Filip Młynarski
Nov 25 '18 at 16:19
2
You seem to be familiar withif/elsestatements - what's holding you back?
– timgeb
Nov 25 '18 at 16:21
add a comment |
if not i1.get(): a = "0"
– Filip Młynarski
Nov 25 '18 at 16:19
2
You seem to be familiar withif/elsestatements - what's holding you back?
– timgeb
Nov 25 '18 at 16:21
if not i1.get(): a = "0"– Filip Młynarski
Nov 25 '18 at 16:19
if not i1.get(): a = "0"– Filip Młynarski
Nov 25 '18 at 16:19
2
2
You seem to be familiar with
if/else statements - what's holding you back?– timgeb
Nov 25 '18 at 16:21
You seem to be familiar with
if/else statements - what's holding you back?– timgeb
Nov 25 '18 at 16:21
add a comment |
2 Answers
2
active
oldest
votes
As li.get() returns a string. Nothing entered will be a string of zero length. You can test for that and then set a to zero. For axample, after your a = i1.get() statement, insert:
if len(a) == 0:
a = "0"
Maybe you also need to handle cases where get() returns values that are not able to be cast to type int. :-)
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
add a comment |
If no value has been entered, i1.get() will have a value of empty string "", so you could just test for that inside showvalues1:
if a == "":
return 0
add a comment |
Your Answer
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%2f53469414%2fhow-do-i-make-my-variable-have-a-default-value-0-when-no-input-has-been-provid%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As li.get() returns a string. Nothing entered will be a string of zero length. You can test for that and then set a to zero. For axample, after your a = i1.get() statement, insert:
if len(a) == 0:
a = "0"
Maybe you also need to handle cases where get() returns values that are not able to be cast to type int. :-)
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
add a comment |
As li.get() returns a string. Nothing entered will be a string of zero length. You can test for that and then set a to zero. For axample, after your a = i1.get() statement, insert:
if len(a) == 0:
a = "0"
Maybe you also need to handle cases where get() returns values that are not able to be cast to type int. :-)
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
add a comment |
As li.get() returns a string. Nothing entered will be a string of zero length. You can test for that and then set a to zero. For axample, after your a = i1.get() statement, insert:
if len(a) == 0:
a = "0"
Maybe you also need to handle cases where get() returns values that are not able to be cast to type int. :-)
As li.get() returns a string. Nothing entered will be a string of zero length. You can test for that and then set a to zero. For axample, after your a = i1.get() statement, insert:
if len(a) == 0:
a = "0"
Maybe you also need to handle cases where get() returns values that are not able to be cast to type int. :-)
answered Nov 25 '18 at 16:48
SteveSteve
16319
16319
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
add a comment |
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
Thankyou, it works ! :D
– Vishvachi Sinha
Nov 25 '18 at 20:36
add a comment |
If no value has been entered, i1.get() will have a value of empty string "", so you could just test for that inside showvalues1:
if a == "":
return 0
add a comment |
If no value has been entered, i1.get() will have a value of empty string "", so you could just test for that inside showvalues1:
if a == "":
return 0
add a comment |
If no value has been entered, i1.get() will have a value of empty string "", so you could just test for that inside showvalues1:
if a == "":
return 0
If no value has been entered, i1.get() will have a value of empty string "", so you could just test for that inside showvalues1:
if a == "":
return 0
answered Nov 25 '18 at 16:27
R. DavidsonR. Davidson
1708
1708
add a comment |
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%2f53469414%2fhow-do-i-make-my-variable-have-a-default-value-0-when-no-input-has-been-provid%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
if not i1.get(): a = "0"– Filip Młynarski
Nov 25 '18 at 16:19
2
You seem to be familiar with
if/elsestatements - what's holding you back?– timgeb
Nov 25 '18 at 16:21