2 回答

TA貢獻1827條經驗 獲得超8個贊
while True: #generic while statement, only way to exit is break
choice = input("Would you like to convert to pounds or kilograms: ") #store our choice in a variable for later
if choice == 'pounds':
kilograms = float(input("Enter the amount of kilograms: "))
pounds = kilograms / 2.205
print('The amount of killograms you entered is ', kilograms,
' This is ', pounds, ' pounds ')
elif choice == "kilograms": #use an elif and define a different specific statement
pounds = float(input("Enter the amount of pounds: "))
kilograms = pounds * 2.2
grams = kilograms * 1000
print('The amount of pounds you entered is ', pounds,
' This is ', kilograms, ' kilograms ', 'and', grams,
'grams' )
elif choice == "quit":
print("Goodbye!")
break #use break to break out of a loop
else: #use an else to define a generic statement
print "%s is not a valid choice"%choice
continue #use continue to return to the beginning of the loop without doing anything after
do_continue = input('Do you want to go again? (y/n) ')
if do_continue == 'y':
pass #do nothing and continue
elif do_continue == 'n':
print("Goodbye!")
break
else:
print("What language is that? We will go again anyways!")

TA貢獻1810條經驗 獲得超4個贊
您的代碼中有一個“錯誤”。
該變量pc
僅在第一次提示時才定義 == 'pounds'
。因此,您會pc is not defined
在第 7 行收到錯誤消息,因為它僅在您輸入“磅”時才被定義。
添加回答
舉報