我知道这很简单,但这些 if 语句仍然很烦人。当我 运行 它总是说长度未定义
I know this is simple but still these if statements are annoying. when I run it always says length is not defined
这是我的代码:
#Starting Point
import time
print("This calculator will convert measurements into a unit of choice")
time.sleep(2)
print("Here are the choice:")
time.sleep(0.5)
print("")
print(" Centimetres, Metres, Kilometres")
time.sleep(0.5)
print("")
print(" Seconds, Minutes, Hours")
time.sleep(0.5)
print("")
print(" Millilitres, litres")
time.sleep(0.5)
print("")
print(" Grams, Kilograms")
time.sleep(0.5)
print("")
#question on convertion
answer = input("Which unit of measurement would you like to use(please enter L for length T for time V for volume and C for capacity?")
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)")
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)")
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)")
#program for Time
elif answer == "T":
time = input("What unit do you want to convert?( S for seconds M for minutes and H for hours)")
print()
if time == "S":
convert_t_1 = ("What do you want to convert seconds into?(M for minutes and H for hours)")
elif time == "M":
convert_t_2 = ("What do you want to convert Minutes to?(S for seconds and H for hours)")
elif time == "H":
convert_t_3 = ("What do you want to convert hours to?(S for seconds and M for minutes)")
#program for volume
elif answer == "V":
volume = input("What unit do you want to convert?(M for millilitres and L for litres)")
print()
if volume == "M":
convert_v_1 = input("Do you want to convert millilitres to litres?(Y for yes and N for no")
elif volume == "L":
convert_v_2 = input("Do you want to convert litres to millilitres?(Y for yes and N for no")
#program for capacity
elif answer == "C":
capacity = input("What unit do you want to convert?( G for grams and K for Kilograms)")
您的问题在这里:
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
如果答案不是 'L',则永远不会定义长度,因为您没有在其他任何地方分配它。请注意,这可能只是一个缩进问题。如果这是您的意图,请确保 "if length..." 语句从检查 answer=="L" 向内缩进。
问题出在您当前的缩进上。
http://www.codeskulptor.org/#user40_pcJ0kOCzXI_0.py
错误的缩进导致变量长度从未被定义并产生错误。
我建议您看一下 PEP 8 -- Python 代码的样式指南:http://legacy.python.org/dev/peps/pep-0008/#indentation
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)")
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)")
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)")
注意缩进和您忘记的输入语句,还添加了 upper() 以便人们可以输入大写或小写响应
#question on convertion
answer = input("Which unit of measurement would you like to use(please enter L for length T for time V for volume and C for capacity?").upper()
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)").upper()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)").upper()
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)").upper()
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)").upper()
#program for Time
elif answer == "T":
time = input("What unit do you want to convert?( S for seconds M for minutes and H for hours)").upper()
if time == "S":
convert_t_1 = input("What do you want to convert seconds into?(M for minutes and H for hours)").upper()
elif time == "M":
convert_t_2 = input("What do you want to convert Minutes to?(S for seconds and H for hours)").upper()
elif time == "H":
convert_t_3 = input("What do you want to convert hours to?(S for seconds and M for minutes)").upper()
#program for volume
elif answer == "V":
volume = input("What unit do you want to convert?(M for millilitres and L for litres)").upper()
这是我的代码:
#Starting Point
import time
print("This calculator will convert measurements into a unit of choice")
time.sleep(2)
print("Here are the choice:")
time.sleep(0.5)
print("")
print(" Centimetres, Metres, Kilometres")
time.sleep(0.5)
print("")
print(" Seconds, Minutes, Hours")
time.sleep(0.5)
print("")
print(" Millilitres, litres")
time.sleep(0.5)
print("")
print(" Grams, Kilograms")
time.sleep(0.5)
print("")
#question on convertion
answer = input("Which unit of measurement would you like to use(please enter L for length T for time V for volume and C for capacity?")
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)")
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)")
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)")
#program for Time
elif answer == "T":
time = input("What unit do you want to convert?( S for seconds M for minutes and H for hours)")
print()
if time == "S":
convert_t_1 = ("What do you want to convert seconds into?(M for minutes and H for hours)")
elif time == "M":
convert_t_2 = ("What do you want to convert Minutes to?(S for seconds and H for hours)")
elif time == "H":
convert_t_3 = ("What do you want to convert hours to?(S for seconds and M for minutes)")
#program for volume
elif answer == "V":
volume = input("What unit do you want to convert?(M for millilitres and L for litres)")
print()
if volume == "M":
convert_v_1 = input("Do you want to convert millilitres to litres?(Y for yes and N for no")
elif volume == "L":
convert_v_2 = input("Do you want to convert litres to millilitres?(Y for yes and N for no")
#program for capacity
elif answer == "C":
capacity = input("What unit do you want to convert?( G for grams and K for Kilograms)")
您的问题在这里:
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
如果答案不是 'L',则永远不会定义长度,因为您没有在其他任何地方分配它。请注意,这可能只是一个缩进问题。如果这是您的意图,请确保 "if length..." 语句从检查 answer=="L" 向内缩进。
问题出在您当前的缩进上。 http://www.codeskulptor.org/#user40_pcJ0kOCzXI_0.py
错误的缩进导致变量长度从未被定义并产生错误。
我建议您看一下 PEP 8 -- Python 代码的样式指南:http://legacy.python.org/dev/peps/pep-0008/#indentation
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)")
print()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)")
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)")
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)")
注意缩进和您忘记的输入语句,还添加了 upper() 以便人们可以输入大写或小写响应
#question on convertion
answer = input("Which unit of measurement would you like to use(please enter L for length T for time V for volume and C for capacity?").upper()
#program for length
if answer == "L":
length = input("What unit do you want to convert?( C for centimetres M for Metres and K for kilometres)").upper()
if length == "C":
convert_l_1 = input("What do you want to convert centimetres into?(M for metres and K for Kilometres)").upper()
elif length == "M":
convert_l_2 = input("What do you want to convert metres into?( C for centimetres and K for kilometres)").upper()
elif length == "K":
convert_l_3 = input("What do you want to convert Kilometres into?(C for centimetres and M for metres)").upper()
#program for Time
elif answer == "T":
time = input("What unit do you want to convert?( S for seconds M for minutes and H for hours)").upper()
if time == "S":
convert_t_1 = input("What do you want to convert seconds into?(M for minutes and H for hours)").upper()
elif time == "M":
convert_t_2 = input("What do you want to convert Minutes to?(S for seconds and H for hours)").upper()
elif time == "H":
convert_t_3 = input("What do you want to convert hours to?(S for seconds and M for minutes)").upper()
#program for volume
elif answer == "V":
volume = input("What unit do you want to convert?(M for millilitres and L for litres)").upper()