我想讓Python提示用戶選擇五個數字並將它們存儲在系統中。到目前爲止,我有:創建五個數字列表
def main():
choice = displayMenu()
while choice != '4':
if choice == '1':
createList()
elif choice == '2':
print(createList)
elif choice == '3':
searchList()
choice = displayMenu()
print("Thanks for playing!")
def displayMenu():
myChoice = '0'
while myChoice != '1' and myChoice != '2' \
and myChoice != '3' and myChoice != '4':
print ("""Please choose
1. Create a new list of 5 integers
2. Display the list
3. Search the list
4. Quit
""")
myChoice = input("Enter option-->")
if myChoice != '1' and myChoice != '2' and \
myChoice != '3' and myChoice != '4':
print("Invalid option. Please select again.")
return myChoice
#This is where I need it to ask the user to give five numbers
def createList():
newList = []
while True:
try:
num = (int(input("Give me five numbers:")))
if num < 0:
Exception
print("Thank you")
break
except:
print("Invalid. Try again...")
for i in range(5):
newList.append(random.randint(0,9))
return newList
一旦我運行該程序,它允許我選擇選項1,並要求用戶輸入五個數字。但是,如果我輸入多個號碼則表示無效,如果我只輸入一個號碼,則表示謝謝並再次顯示該菜單。我哪裏錯了?
謝謝。你介意讓我看看會是什麼樣子? –