簡體   English   中英

如何將 go 再次返回到 python 中的變量

[英]How to go back again to an variable in python

下面的代碼只是一個例子,我想知道每個選項后,它再次返回gg誰能幫助我?

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

提前致謝!

你可以把你的代碼放在一個while loop中,然后在每個任務之后從用戶那里得到gg ,就像這樣,當然在一個條件下打破循環:

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

我相信在行前

while True:

在提供的代碼塊之前,然后將提供的代碼塊縮進一級很可能會導致執行返回到gg=int(input(f""" ... 所需的語句。

就像是:

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

...等等。

最好的方法之一是將所有代碼放入不同的函數中,然后在主 function 中調用它們。 這是一些示例代碼

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM