我对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