[英]how to use if condition on more than one line?
I'm new to python so I have 1 question about the "if" sentence.我是 python 的新手,所以我有 1 个关于“如果”句子的问题。 I have 3 variables and I need to show 1 message on the screen like the millage is" ", look:
我有 3 个变量,我需要在屏幕上显示 1 条消息,就像 millage 是“”,看:
I have 3 city if i write 1 show the millage but if don't exist show "address not found try again" when I use else like the below:我有 3 个城市如果我写 1 显示 millage 但如果不存在显示“找不到地址再试一次”当我使用其他如下所示:
else: print(Fore.GREEN + "\nAddress not Found, check address again") else: print(Fore.GREEN + "\n没有找到地址,再次检查地址")
show me the sentence "not found check again" but show the millage too, I don't know if I'm explaining ok, but if someone can help ill B thankful.给我看“未找到再次检查”这句话,但也显示了 millage,我不知道我的解释是否正确,但如果有人能帮助生病 B 感激不尽。
destino1 = "Tuscalosa AL"
destino2 = 'Warren OH'
destino3 = "Atlanta GA"
if address in (destino1):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m","935 miles")
if address in (destino2):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1600 miles")
if address in (destino3):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1107 miles")
else:
print(Fore.GREEN + "\nAddress not Found, check address again")
I think what you want is this kind of structure:我想你想要的是这种结构:
if address in (destino1):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m","935 miles")
elif address in (destino2):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1600 miles")
elif address in (destino3):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1107 miles")
else:
print(Fore.GREEN + "\nAddress not Found, check address again")
Use if - elif - else
format.使用
if - elif - else
格式。
If I understood you question correctly, you are trying to print milage for a city entered by user but if the city entered is not from the given three, you want to print "not found" statement.如果我理解你的问题是正确的,你正在尝试打印用户输入的城市的里程数,但如果输入的城市不是给定的三个城市,你想打印“未找到”声明。
you can do something like this,你可以这样做,
if address==destion1:
print(milage for destion1)
elif address==destion2:
print(milage for destion2)
elif address==destion3:
print(milage for destion3)
else:
print("Not found statement")
Additional suggestions It will be a good practice to use a dict or list instead of declaring 3 variables.其他建议使用字典或列表而不是声明 3 个变量是一种很好的做法。 And you can shorten the conditional statements, and overall code, like below,
你可以缩短条件语句和整体代码,如下所示,
destinations = {"dest1" : "milage1", "dest2" : "milage2", "dest3" : "milage3"}
if address in destinations:
print(f"Milage for {address} is {destinations[address]})
else:
print("Destination not found")
use this:用这个:
if address in (destino1):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m","935 miles")
elif address in (destino2):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1600 miles")
elif address in (destino3):
print(Fore.WHITE + "\nTotal Miles ", "\033[34m", "1107 miles")
else:
print(Fore.GREEN + "\nAddress not Found, check address again")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.