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 行收到错误消息,因为它仅在您输入“磅”时才被定义。
添加回答
举报