如果输入不相等,则重新启动代码



这是我的代码,我希望它在输入不相等时重新启动。阅读下面的代码了解更多信息。


import colorama
import os
import time
from colorama import Fore
text = Fore.BLUE + """
██████╗░██████╗░░█████╗░██████╗░███████╗███╗░░██╗
██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝████╗░██║
██████╦╝██████╔╝███████║██║░░██║█████╗░░██╔██╗██║
██╔══██╗██╔══██╗██╔══██║██║░░██║██╔══╝░░██║╚████║
██████╦╝██║░░██║██║░░██║██████╔╝███████╗██║░╚███║
╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚══════╝╚═╝░░╚══╝
"""
print(text)
inp = input(Fore.WHITE + "What would you like to do?: ")
while inp == 'ping' or 'Ping':
os.system("cls")
count = 1
e = input("Enter IP Address:   ")
replies = 0
replies += 1
hostname = e
os.system("cls")
colors = list(vars(colorama.Fore).values())
lr = random.choice(colors)
lr2 = random.choice(colors)
lr3 = random.choice(colors)
lr4 = random.choice(colors)
print("-" * 100)
print("=" * 100)
print("-" * 100)
while True:
response = os.system(
"ping -n 1 " + hostname + " >nul")
if response == 0:
print(lr4 + "~ " + lr + hostname + lr2 + " is online!" + lr3 + " [" + str(count) + "]" + lr4 + " ~")
else:
print(Fore.BLUE + hostname + Fore.GREEN + " is offline!" + Fore.RED + " [" + str(count) + "]")
count += 1
time.sleep(.02)
continue
while inp != 'ping' or 'Ping':
print(inp + ' is not a valid command!')
continue

我想让它说这不是一个有效的命令,然后问他们想再做什么?也许还有一段时间,我不知道。

只需用一个函数包装它:

import colorama
import os
import time
from colorama import Fore
def runCode(inp):
os.system("cls")
count = 1
e = input("Enter IP Address:   ")
replies = 0
replies += 1
hostname = e
os.system("cls")
colors = list(vars(colorama.Fore).values())
lr = random.choice(colors)
lr2 = random.choice(colors)
lr3 = random.choice(colors)
lr4 = random.choice(colors)
print("-" * 100)
print("=" * 100)
print("-" * 100)
while True:
response = os.system(
"ping -n 1 " + hostname + " >nul")
if response == 0:
print(lr4 + "~ " + lr + hostname + lr2 + " is online!" + lr3 + " [" + str(count) + "]" + lr4 + " ~")
else:
print(Fore.BLUE + hostname + Fore.GREEN + " is offline!" + Fore.RED + " [" + str(count) + "]")
count += 1
time.sleep(.02)
continue
inp=""
while inp != 'ping' or 'Ping':
text = Fore.BLUE + """
██████╗░██████╗░░█████╗░██████╗░███████╗███╗░░██╗
██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝████╗░██║
██████╦╝██████╔╝███████║██║░░██║█████╗░░██╔██╗██║
██╔══██╗██╔══██╗██╔══██║██║░░██║██╔══╝░░██║╚████║
██████╦╝██║░░██║██║░░██║██████╔╝███████╗██║░╚███║
╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚══════╝╚═╝░░╚══╝
"""
print(text)
inp = input(Fore.WHITE + "What would you like to do?: ")
print(inp + ' is not a valid command!')
continue
runCode(inp)

如果你只需要在开始时重新启动代码(输入(,你可以这样写:

import colorama
import os
import time
from colorama import Fore
text = Fore.BLUE + """
██████╗░██████╗░░█████╗░██████╗░███████╗███╗░░██╗
██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔════╝████╗░██║
██████╦╝██████╔╝███████║██║░░██║█████╗░░██╔██╗██║
██╔══██╗██╔══██╗██╔══██║██║░░██║██╔══╝░░██║╚████║
██████╦╝██║░░██║██║░░██║██████╔╝███████╗██║░╚███║
╚═════╝░╚═╝░░╚═╝╚═╝░░╚═╝╚═════╝░╚══════╝╚═╝░░╚══╝
"""
print(text)
inp = ""
while inp != 'ping' or 'Ping':
inp = input(Fore.WHITE + "What would you like to do?: ")

if inp != 'ping' or 'Ping':
print("Try again(or something)")
if inp == 'ping' or 'Ping':
os.system("cls")
count = 1
e = input("Enter IP Address:   ")
replies = 0
replies += 1
hostname = e
os.system("cls")
colors = list(vars(colorama.Fore).values())
lr = random.choice(colors)
lr2 = random.choice(colors)
lr3 = random.choice(colors)
lr4 = random.choice(colors)
print("-" * 100)
print("=" * 100)
print("-" * 100)
while True:
response = os.system(
"ping -n 1 " + hostname + " >nul")
if response == 0:
print(lr4 + "~ " + lr + hostname + lr2 + " is online!" + lr3 + " [" + str(count) + "]" + lr4 + " ~")
else:
print(Fore.BLUE + hostname + Fore.GREEN + " is offline!" + Fore.RED + " [" + str(count) + "]")
count += 1
time.sleep(.02)
continue

最新更新