Home > Mobile >  How to go back again to an variable in python
How to go back again to an variable in python

Time:08-15

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!

CodePudding user response:

You can put your code in a while loop and then get gg from the user after every task, something like this, and of course breaks the loop at a condition:

while True:
    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')
        break

CodePudding user response:

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

CodePudding user response:

I believe that prepending the line

while True:

before the provided code block and then indenting the provided code block one level would very likely cause execution to return to the gg=int(input(f"""... statement as desired.

Something like:

while True:
  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"))

...et cetera.

  • Related