这个问题已经在这里有了答案: > How to test multiple variables against a value? 21个
我是python的新手,我的任务是编写厘米到英寸,反之亦然.这个想法是用户输入一个数字,程序询问他们是否要将其从英寸转换为厘米或将厘米转换为英寸,然后显示结果.
我在Mac上使用python 3.3.5很有帮助.
这是我的代码:(注释掉的部分显示了任务和我的第一次尝试)
##
##Write a program that converts between centimetres, and inches and vice versa, by:
##asking the user to input a number
##asking the user to choose between converting from centimetres to inches or from inches to centimetres
##calculating and outputting the result using functions
##### Old attempt
##print(" Program to convert beteween centimetres and inches " )
##
##centimetre = 1
##inch = centimetre * 2.45
##
##number = input (" Please enter a number: ")
##choice = input (" Do you want to convert into centimetres or inches: ")
##if choice == "centimetres" or "Centimetres":
## print("Your number is", centimetre*number, "cm")
##else:
## print("Your number is",number*inch, "inches")
number = int(input(" Please enter a number: "))
choice = input(str(" If you want to convert from centimetres to inches type 'A' \n If you want to convert from inches to centimetres type 'B': "))
if choice in "A" or "a":
print(" Your number is" , number*2.54,"inches")
else:
print(" Your number is", number/2.54, "centimetres")
找到答案,谢谢您的快速答复!
解决方法:
例如,如果将“ A”或“ a”中的条件“选择”更改为“如果选择”中的“ Aa”:条件,则可以测试选择是“ A”还是“ a”.原始版本始终评估为True.
标签:python