系統要求我將英尺轉換為英寸,碼轉換為英尺,數英里轉換為碼,將英里轉換為英尺,然后打印輸出。當用戶選擇一個菜單選項,然后選擇英尺數時,該函數應該計算分配給它的分配代碼,返回其值并打印結果。我有下面代碼的一部分。我知道如果我不能讓它打印第一組代碼,那么它也不會打印任何其他選擇。我得到的錯誤是在“如果選擇== 1:”錯誤說它是無法訪問的。任何幫助是值得贊賞的。choice = int(input("Please choose a menu option: "))choice2 = int(input("Enter the number of feet: "))def feet_to_inches(userFeet): inches = "userFeet / 1" * 12 return inches if choice == 1: feet_to_inches = userFeet print(feet_to_inches)
1 回答

楊魅力
TA貢獻1811條經驗 獲得超6個贊
它無法訪問,因為您正在結束函數,然后才能使用if語句訪問if語句
return inches
相反,在 if 語句完成后使用它
def feet_to_inches(userFeet):
inches = "userFeet / 1" * 12
if choice == 1:
feet_to_inches = userFeet
print(feet_to_inches)
return inches
添加回答
舉報
0/150
提交
取消