Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to go back again to an variable in python

the code below is just an example! I want to know after every options, it again return to gg Can anyone help me?

gg=int(input(f"""
[1] Age Calc    [2] Name lenth    

[3] Calculator  [4] {FORE.RED}Exit{FORE.RESET} 

Option: """))
d1 = dt.today()
year = int(d1 .strftime("%Y"))
if gg == 1:
   birth_year = int(input("Enter your birth year:\n"))
   age = int(f"{year - birth_year}")
   print(f"You are {Fore.GREEN}{age}{Fore.RESET} years old.")
   print(f"And you are {Fore.GREEN}{age + 2}{FORE.RESET} years old in {Style.DIM}{FORE.LIGHTMAGENTA_EX}Korea{FORE.RESET}{Style.RESET_ALL} as they count since you were in yo mama's tummy like a ball.")
  

# namelength
if gg == 2:
 print("\nNow let's find how many characters are there in your name!")
 name = input("Enter your name:\n")
 print(
    f"Your name is {FORE.MAGENTA}{name.capitalize()}{FORE.RESET} and it contains {FORE.RED}{len(name)}{FORE.RESET} characters.")

 print(
    f"\nNow you have your age and name length at your fingertips {FORE.MAGENTA}yay!{FORE.RESET}")

# calc
if gg == 3:
 print("Now let's get calculating:\n")
 fnum = int(input("Enter the first number:\n"))
 snum = int(input("Enter the second number:\n"))
 task = str(input("Now what do you want to do?\nPress + for addition, press - for substraction, press * for multiplication, press / for division\n"))
 while task != "+" and task != "-" and task != "*" and task != "/":
   print(f"{FORE.RED}{task}is not a valid input!{FORE.RESET} Please enter a valid input.")
   task = str(input("What do you want to do?\nPress + for addition, press - for substraction, press * for multiplication, press / for division\n"))
 if task == "+":
   animation = "|/-\\"
   idx = 0
   while True:
    
    ok = print(f"Calculating... {fnum} + {snum}", animation[idx %
          len(animation)], end="\r")
    sleep(0.1)
    if idx == 2*8:
     break
    idx += 1
   os.system('clear')
   print(f"Calculating... {fnum} + {snum}")
   print(f"The addition results in {fnum + snum}")
    


 if task == "-":
   animation = "|/-\\"
   idx = 0
   while True:
    
    ok = print(f"Calculating... {fnum} - {snum}", animation[idx %
          len(animation)], end="\r")
    sleep(0.1)
    if idx == 2*8:
     break
    idx += 1
   os.system('clear')
   print(f"Calculating... {fnum} - {snum}")
   print(f"The substraction results in {fnum - snum}")


 if task == "*":
   animation = "|/-\\"
   idx = 0
   while True:
    
    ok = print(f"Calculating... {fnum} * {snum}", animation[idx %
          len(animation)], end="\r")
    sleep(0.1)
    if idx == 2*8:
     break
    idx += 1
   os.system('clear')
   print(f"Calculating... {fnum} * {snum}")
   print(f"The multiplications results in {fnum * snum}")

   
 if task == "/":
   animation = "|/-\\"
   idx = 0
   while True:
    
    ok = print(f"Calculating... {fnum} / {snum}", animation[idx %
          len(animation)], end="\r")
    sleep(0.1)
    if idx == 2*8:
     break
    idx += 1
   os.system('clear')
   print(f"Calculating... {fnum} / {snum}")
   print(f"The addition results in {fnum / snum}")
   retrn = (input("Would you Like to Restart? y/n"))

if gg == 4:
  print(f"{FORE.RED}Exiting....{FORE.RESET}")
  os.system('kill 1')

Thanks in advance!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

one of the best ways of doing this would be to put all of your code into different functions and then call them in your main function. Here is some sample code

    def game():
    gg = int(input(f"""
    [1] Age Calc    [2] Name lenth    

    [3] Calculator  [4] {FORE.RED}Exit{FORE.RESET} 

    Option: """))
    d1 = dt.today()
    year = int(d1.strftime("%Y"))
    if gg == 1:
        birth_year = int(input("Enter your birth year:\n"))
        age = int(f"{year - birth_year}")
        print(f"You are {Fore.GREEN}{age}{Fore.RESET} years old.")
        print(
            f"And you are {Fore.GREEN}{age + 2}{FORE.RESET} years old in {Style.DIM}{FORE.LIGHTMAGENTA_EX}Korea{FORE.RESET}{Style.RESET_ALL} as they count since you were in yo mama's tummy like a ball.")

    # namelength
    if gg == 2:
        print("\nNow let's find how many characters are there in your name!")
        name = input("Enter your name:\n")
        print(
            f"Your name is {FORE.MAGENTA}{name.capitalize()}{FORE.RESET} and it contains {FORE.RED}{len(name)}{FORE.RESET} characters.")

        print(
            f"\nNow you have your age and name length at your fingertips {FORE.MAGENTA}yay!{FORE.RESET}")

    # calc
    if gg == 3:
        print("Now let's get calculating:\n")
        fnum = int(input("Enter the first number:\n"))
        snum = int(input("Enter the second number:\n"))
        task = str(input(
            "Now what do you want to do?\nPress + for addition, press - for substraction, press * for multiplication, press / for division\n"))
        while task != "+" and task != "-" and task != "*" and task != "/":
            print(f"{FORE.RED}{task}is not a valid input!{FORE.RESET} Please enter a valid input.")
            task = str(input(
                "What do you want to do?\nPress + for addition, press - for substraction, press * for multiplication, press / for division\n"))
        if task == "+":
            animation = "|/-\\"
            idx = 0
            while True:

                ok = print(f"Calculating... {fnum} + {snum}", animation[idx %
                                                                        len(animation)], end="\r")
                sleep(0.1)
                if idx == 2 * 8:
                    break
                idx += 1
            os.system('clear')
            print(f"Calculating... {fnum} + {snum}")
            print(f"The addition results in {fnum + snum}")

        if task == "-":
            animation = "|/-\\"
            idx = 0
            while True:

                ok = print(f"Calculating... {fnum} - {snum}", animation[idx %
                                                                        len(animation)], end="\r")
                sleep(0.1)
                if idx == 2 * 8:
                    break
                idx += 1
            os.system('clear')
            print(f"Calculating... {fnum} - {snum}")
            print(f"The substraction results in {fnum - snum}")

        if task == "*":
            animation = "|/-\\"
            idx = 0
            while True:

                ok = print(f"Calculating... {fnum} * {snum}", animation[idx %
                                                                        len(animation)], end="\r")
                sleep(0.1)
                if idx == 2 * 8:
                    break
                idx += 1
            os.system('clear')
            print(f"Calculating... {fnum} * {snum}")
            print(f"The multiplications results in {fnum * snum}")

        if task == "/":
            animation = "|/-\\"
            idx = 0
            while True:

                ok = print(f"Calculating... {fnum} / {snum}", animation[idx %
                                                                        len(animation)], end="\r")
                sleep(0.1)
                if idx == 2 * 8:
                    break
                idx += 1
            os.system('clear')
            print(f"Calculating... {fnum} / {snum}")
            print(f"The addition results in {fnum / snum}")
            retrn = (input("Would you Like to Restart? y/n"))

    if gg == 4:
        print(f"{FORE.RED}Exiting....{FORE.RESET}")
        os.system('kill 1')
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    run = true
    while run:
        game()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading