我正在为python编写一个程序,它应该将二进制转换为等效的十六进制和十进制值。问题是我需要设置一个循环,以便整个程序重复,直到用户选择退出选项。继承我的代码:
denary = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
binary = ["0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"]
hexadecimal = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"]
print("What would you like to do today?\n1. Print values\n2. Convert denary values between 0 and 15 to binary and hex values\n3. Convert hex values between 0 and 15 to binary and denary values\n9. Quit")
option = int(input("Input Choice: "))
if option == 1:
print("denary", "binary", "hexadecimal")
for row in zip(denary, binary, hexadecimal):
print (" ".join(row))
elif option == 2:
denary = int(input("Enter a denary number: "))
index = denary
print("Binary: ", binary[index])
print("Hexadecimal: ", hexadecimal[index])
elif option == 3:
hex=(input("Enter a hex number: "))
if hex=="a":
print("Denary: ", denary[10])
print("Binary: ", binary[10])
elif hex=="b":
print("Denary: ", denary[11])
print("Binary: ", binary[11])
elif hex=="c":
print("Denary: ", denary[12])
print("Binary: ", binary[12])
elif hex=="d":
print("Denary: ", denary[13])
print("Binary: ", binary[13])
elif hex=="e":
print("Denary: ", denary[14])
print("Binary: ", binary[14])
elif hex=="f":
print("Denary: ", denary[15])
print("Binary: ", binary[15])
elif hex=="1":
print("Denary: ", denary[1])
print("Binary: ", binary[1])
elif hex=="2":
print ("Denary: ", denary[2])
print ("Binary: ", binary[2])
elif hex=="3":
print ("Denary: ", denary[3])
print ("Binary: ", binary[3])
elif hex=="4":
print ("Denary: ", denary[4])
print ("Binary: ", binary[4])
elif hex=="5":
print ("Denary: ", denary[5])
print ("Binary: ", binary[5])
elif hex=="6":
print ("Denary: ", denary[6])
print ("Binary: ", binary[6])
elif hex=="7":
print ("Denary: ", denary[7])
print ("Binary: ", binary[7])
elif hex=="8":
print ("Denary: ", denary[8])
print ("Binary: ", binary[8])
elif hex=="9":
print ("Denary: ", denary[9])
print ("Binary: ", binary[9])
elif option ==9:
quit()
是否有人知道在何处放置循环以便重复直到选择退出选项?
感谢。
答案 0 :(得分:1)
就这样做吧
your_function
Huge_loop
quit()
所以它只会在循环运行时退出。
答案 1 :(得分:0)
好吧,因为每件事都在全球范围内,我会编写另一个Python脚本来运行第一个:
# run main script 10 times
for i in range(10):
execfile('main.py')