Reputation: 3
First time poster here. I'm trying to create a simple conversion program on Python for a project I am working on as part of a course I am enrolled in.
I am trying to return the conversion for a number of length units based on a starting unit. To do this I am trying to use nested dictionaries. It has to be via a CLI terminal so i'm not allowed to use ttinker or anything like that to provide a GUI.
I am trying to return it in the format:
x centimetres converted equals:
x kilometres
x metres
x miles etc.
Instead I am getting this return with my current version of code(although I do expect it to return like this, but this is the closest to the result I want I have came yet):
x centimetres converted equals:
x
x
x
kilometres
metres
miles
My code at present looks like:
len_selection = pyip.inputMenu(["Nanometre", "Micrometre", "Millimetre", "Centimetre", "Metre", "Kilometre", "Inch", "Foot", "Yard", "Mile", "Nautical Mile"], numbered=True)
print("\nEnter the number of your required unit\n")
len_value = pyip.inputNum("\nPlease enter the length to convert:")
length_conversion_factors = {
'kilometre': {'metre': 0.001, 'centimetre': 0.00001, 'millimetre': 0.000001,
'micrometre': 0.000000001, 'nanometre': 0.000000000001, 'mile': 1.60934,
'yard': 0.000914397727272727, 'foot': 0.000304799242424242, 'inch': 2.53999368686869,
'nautical mile': 1.852},
'metre': {'kilometre': 1000, 'centimetre': 0.01, 'millimetre': 0.001, 'micrometre': 0.000001,
'nanometre': 0.000000001, 'mile': 1609.34, 'yard': 0.914397727272727, 'foot': 0.304799242424242,
'inch': 0.0253999368686869, 'nautical mile': 1852},
'centimetre': {'kilometre': 100000, 'metre': 100, 'millimetre': 0.1,
'micrometre': 0.0001, 'nanometre': 0.0000001, 'mile': 160934,
'yard': 91.4397727272727, 'foot': 30.4799242424242, 'inch': 2.53999368686869,
'nautical mile': 185200}}
length_key = len_selection.lower()
print(f"\n{len_value} {len_selection}s converted equals:")
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value)
for key in length_conversion_factors[length_key]:
print(key)
I have spent a day and a half on this already and I am pulling my hair out at this point.
I have tried the likes of 'for key, value in length_conversion_factors[length_key]:'so it all runs in the one for loop and I get a Value error saying too much to be unpacked. I also tried unpacking the keys first, storing them in a variable and then using a print statement but no joy with it either.
I've tried using 'return key' and 'return conv_value' and then using a separate print statement on those but cant seem to get it to work at all.
I' know I'm probably missing something very simple. many thanks in advance
Upvotes: 0
Views: 35
Reputation: 5409
Python lets you print more than one thing in a statement:
if length_key in length_conversion_factors:
for value in length_conversion_factors[length_key]:
conv_value = len_value * float(length_conversion_factors[length_key][value])
print(conv_value, value)
This is giving me output like:
1 kilometres converted equals:
0.001 metre
1e-05 centimetre
1e-06 millimetre
1e-09 micrometre
which appears to be what you wanted. You can even replace the final print statement with an f-string, just like the one that appears already in the code: print(f"{conv_value} {value}s")
Upvotes: 1