英寸和厘米的交互python_Python的新手-尝试将厘米编码为英寸,反之亦然
這個問題已經在這里有了答案:????????????>????????????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
總結
以上是生活随笔為你收集整理的英寸和厘米的交互python_Python的新手-尝试将厘米编码为英寸,反之亦然的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python字符数组输出_python字
- 下一篇: dataframe scala 修改值_