Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:23:16+00:00 2026-06-17T16:23:16+00:00

Is there any type of variable that allows me to increase its value, without

  • 0

Is there any type of variable that allows me to increase its value, without the memorized value being overwritten? Example:

def main():
var = 0

  inc = input("Want to increase your variable?")
    if inc == "yes":
      var = var + 1
      main()
      #restart the script from the top
    if inc == "no":
      exit()

When the code restarts, the var will return to its value of “0” without having remembered that “var = var + 1” bit we did, is there any variable type that will update itself to match the changes made in the script?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:23:18+00:00Added an answer on June 17, 2026 at 4:23 pm

    When you run your function, main(), you’re resetting var back to 0. Notice how it’s inside the function, and so whatever is inside a function will be run.

    Here var won’t be changed to 0 every time:

    var = 0
    def main(number):
        print number
        inc = raw_input("Want to increase your variable?") # Or input() if python 3.x
        if inc == "yes":
            number += 1
            return main(number)
        if inc == "no":
            return number
    
    var = main(var)
    print var
    

    var in this case is a global variable. It can be accessed throughout the whole code.

    The code above, when run:

    python file.py
    0
    Want to increase your variable?yes
    1
    Want to increase your variable?yes
    2
    Want to increase your variable?no
    2 #result of printing var at the end
    

    I hope this answered your problems. If you have any other questions, I’ll try answer them ;). I’m a bit of a novice to python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any standalone type conversion libraries? I have a data storage system that
Is there any primitive data type that it's safe to not initialize? How about
Is there any way to change(increase) the data type of a column while saving
Is there any way that I can detect browser type on routing and use
Is there any way to accept POST type requests without using Nerve lib in
Is there a valid class Type variable that can be used with the instanceof
Is there any difference between type casting & type conversion in c++.
Is there any programming language (or type system) in which you could express the
Is there any convenient way to type in braces in Math in Latex? Especially
Is there any way to store the generic parameter type passed in at construction

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.