How do I make my variable have a default value “0” when no input has been provided by the user in python?












-1















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()









share|improve this question























  • if not i1.get(): a = "0"

    – Filip Młynarski
    Nov 25 '18 at 16:19






  • 2





    You seem to be familiar with if/else statements - what's holding you back?

    – timgeb
    Nov 25 '18 at 16:21
















-1















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()









share|improve this question























  • if not i1.get(): a = "0"

    – Filip Młynarski
    Nov 25 '18 at 16:19






  • 2





    You seem to be familiar with if/else statements - what's holding you back?

    – timgeb
    Nov 25 '18 at 16:21














-1












-1








-1








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()









share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 with if/else statements - 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






  • 2





    You seem to be familiar with if/else statements - 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












2 Answers
2






active

oldest

votes


















0














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. :-)






share|improve this answer
























  • Thankyou, it works ! :D

    – Vishvachi Sinha
    Nov 25 '18 at 20:36



















1














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





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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. :-)






    share|improve this answer
























    • Thankyou, it works ! :D

      – Vishvachi Sinha
      Nov 25 '18 at 20:36
















    0














    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. :-)






    share|improve this answer
























    • Thankyou, it works ! :D

      – Vishvachi Sinha
      Nov 25 '18 at 20:36














    0












    0








    0







    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. :-)






    share|improve this answer













    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. :-)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 25 '18 at 16:48









    SteveSteve

    16319




    16319













    • 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





    Thankyou, it works ! :D

    – Vishvachi Sinha
    Nov 25 '18 at 20:36













    1














    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





    share|improve this answer




























      1














      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





      share|improve this answer


























        1












        1








        1







        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





        share|improve this answer













        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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 16:27









        R. DavidsonR. Davidson

        1708




        1708






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)