似乎無法讓此循環工作,它保持循環回到二進制數字輸入。我希望它回到菜單選擇。對於noob問題抱歉,我是python和編程新手。整數輸入驗證Python的
import sys
loop = 0
menu_Select = 0
for menu_Select in range(1,100):
#Display user options to the screen
print('*** Menu ***')
print('1. Convert to binary')
userMenu = input('What would you like to do [1,2,3,4]? ')
if userMenu != '1' and userMenu != '2' and userMenu != '3' and userMenu != '4':
print("Please enter either 1, 2, 3, or 4.")
elif userMenu == '4':
print('Goodbye.')
sys.exit(0)
elif userMenu == '1':
#Decimal to Binary convertion code
print('\n')
while loop < 1:
while True:
try:
user_Number = (int(input('Please enter number: ')))
except ValueError:
print('wrong')
else:
binary_num = []
while (user_Number > 0):
if user_Number % 2 != 0:
binary_num.append(1)
elif user_Number % 2 == 0:
binary_num.append(0)
user_Number = user_Number // 2
binary_num.reverse()
binary_display = ''.join(str(k) for k in binary_num)
print('Binary number: ',binary_display)
loop += 1
你什麼時候期待'while True:'循環結束? – Matthias
顯示二進制數後 – girthquake
我不允許使用break語句 – girthquake