因此,我試圖在我的主程序中將變量設置爲模塊中函數的整數輸入。我無法解決如何做到這一點。如何從模塊中的輸入設置變量
這是我的主要程序。菜單是我的模塊的名稱,因爲我用它來顯示菜單。 'menulist'是你說你想要菜單顯示的地方。那部分工作正常。
import time, sys
from menu import *
menulist = ['Say hi', 'Say bye', 'Say yes', 'Say no', 'Exit']
choice = int(0)
menu(menulist)
choosing(menulist, choice)
print(choice) ##This is so I can see what it is
if choice == 1:
print('Say hi')
elif choice == 2:
print('Say bye')
elif choice == 3:
print('Say yes')
elif choice == 4:
print('Say no')
elif choice == 5:
sys.exit()
else: ##This will print if choice doesn't equal what I want
print('Error')
這是我的模塊。
import time
def menu(menulist):
print('Menu:')
time.sleep(1)
x = 0
while x < len(menulist):
y = x + 1
printout = ' ' + str(y) + '. ' + menulist[x]
print(printout)
x = x + 1
time.sleep(0.3)
time.sleep(0.7)
print('')
def choosing(menulist, choice):
flag = True
while flag:
try:
choice = int(input('Enter the number of your choice: '))
time.sleep(0.8)
if choice < 1 or choice > len(menulist):
print('That wasn\'t an option sorry')
time.sleep(1)
else:
flag = False
except ValueError:
print('That wasn\'t an option sorry')
time.sleep(1)
菜單功能工作正常,以及選擇功能,幾乎做什麼,我希望它,但它不會在我的主要程序中設置「選擇」時,我把它從我的模塊輸入。對不起,如果它是明顯的東西,我是編程新手。謝謝
非常感謝。我是編程新手,所以我沒有真正理解這一切,但這確實更有意義。我在我周圍亂搞,但試圖讓它起作用,因此像初始化選擇一樣。再次感謝 –