[英]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.