Reputation:
I have the following input which contains Aircraft and Services
AirCrafts={'Cargo Aircraft':'1','International Aircraft':'2','Domestic Aircraft':'3'}
Services={
'AirCrafts':[1,2,3],
'Cargo Aircraft':[
"Fedx","DHFL","Emirates SkyCargo","Qatar Airways Cargo","Cathay Pacific Cargo"
],
'International Aircraft':[
"Air India","Air France","BA"
],
'Domestic Aircraft':[
"TruJet","Indigo","AirAsia"
]
}
I have a generated sentence the following sentence using SenGen Function.
def SenGen(alpha,beta):
for keys,values in alpha.items():
for key,value in beta.items():
if key in keys:
if values == []:
print(f"{keys} are not found.")
if len(values) > 1:
print(f"{keys} are ", end="\n")
for i, val in enumerate(values):
if len(values) == 1:
if "Not found" in value:
print(f"{keys} are {val}. ", end="\n")
else:
print(f"{keys} is {val}. ", end = "\n")
else:
if i == len(values)-1:
print(f"•{val}. ", end="\n")
elif i == len(value)-2:
print(f"•{val} ", end="\n")
else:
print(f"•{val}, ", end="\n")
My generated output is below after running SenGen(Services,AirCrafts)
.
SenGen(Services,AirCrafts)
International Aircraft are
•Air India,
•Air France,
•BA.
Domestic Aircraft are
•TruJet,
•Indigo,
•AirAsia.
In the above output, I have international Aircraft are and Domestic Aircraft are. in place of International Aircraft, I want to generate a proper sentence such that my output must look like
International Aircrafts that run from the various airport of India are
for the Domestic Aircrafts
Domestic Aircraft which run in within India are
How can I generate a proper sentence as shown above?
Upvotes: 0
Views: 155
Reputation: 8508
In your code, you need to plug in the additional text. Otherwise the program will not know that you want these texts added.
if len(values) > 1:
if "International" in keys:
print(f"{keys} that run from the various airports of India are ", end="\n")
elif "Domestic" in keys:
print(f"{keys} which run in within India are ", end="\n")
else:
print(f"{keys} are ", end="\n")
Here's the code that provides you a return statement.
def SenGen(alpha,beta):
temp = '' #store the results for each iteration of `AirCrafts`
for keys,values in alpha.items():
if keys == 'International Aircraft':
temp += '\n' + keys + ' that run from the various airports of India are :\n' + ',\n'.join(beta[keys])
elif keys == 'Domestic Aircraft':
temp += '\n' + keys + ' which run within India are :\n' + ',\n'.join(beta[keys])
elif keys == 'Cargo Aircraft':
temp += '\n' + keys + ' are :\n' + ',\n'.join(beta[keys])
else:
temp += '\n' + keys + ' are not found'
temp += '\n'
return temp
x = SenGen(AirCrafts,Services)
print (x)
If you don't want the results to be printed separately on each line, you can remove the \n
from the string.
Output for your reference:
>>> print (x)
Cargo Aircraft are :
Fedx,
DHFL,
Emirates SkyCargo,
Qatar Airways Cargo,
Cathay Pacific Cargo
International Aircraft that run from the various airports of India are :
Air India,
Air France,
BA
Domestic Aircraft which run within India are :
TruJet,
Indigo,
AirAsia
>>> x
'\nCargo Aircraft are :\nFedx,\nDHFL,\nEmirates SkyCargo,\nQatar Airways Cargo,\nCathay Pacific Cargo\n\nInternational Aircraft that run from the various airports of India are :\nAir India,\nAir France,\nBA\n\nDomestic Aircraft which run within India are :\nTruJet,\nIndigo,\nAirAsia\n'
>>>
Upvotes: 1
Reputation: 9018
So basically this is the line which needed change as per your requirement
if len(values) > 1:
print(f"{keys} are ", end="\n")
You can change it to this, and you will be good to go:
if len(values) > 1:
# for international the statement is different
if keys == "International Aircraft":
print(f"{keys} that run from the various airport of India are ", end="\n")
elif keys == "Domestic Aircraft":
print(f"{keys} which run in within India are", end="\n")
else:
# your else print
Additional Information
To return
the statement, do not do like this, you need to use String Concatenation. So in order to do the return, do this in place of print
if len(values) > 1:
# for international the statement is different
if keys == "International Aircraft":
return str(keys) + " that run from the various airport of India are \n"
elif keys == "Domestic Aircraft":
return str(keys) + " which run in within India are \n"
else:
# your else print
Upvotes: 1