我對Python(和編程)很陌生,只是試着做一個程序來轉換小數。我已經定義了一個函數,因爲我希望稍後在程序中重用它,但是我有一個問題將函數的結果傳遞給程序的其餘部分。從函數返回值
print "For decimal to binary press B"
print "For decimal to octal press O"
print "For decimal to hexadecimal press H"
def checker(n):
choice = n
while choice not in ("B", "O", "H"):
print "That is not a choice."
choice = str.upper(raw_input("Try again: "))
else:
return choice
firstgo = str.upper(raw_input("Enter your choice: "))
checker(firstgo)
if choice == 'B':
n = int(raw_input("Enter the number to be converted to binary: "))
f = bin(n)
print n, "in binary is", f[2:]
elif choice == 'O':
n = int(raw_input("Enter the number to be converted to octal: "))
f = oct(n)
print n, "in octal is", f[1:]
elif choice == 'H':
n = int(raw_input("Enter the number to be converted to hexadecimal: "))
f = hex(n)
print n, "in hexadecimal is", f[2:]
什麼是你的問題/問題/堆棧跟蹤? – bereal 2014-11-23 19:13:26