将新号码存储在全局列表中,每次添加更多号码



我目前正在尝试让我的代码从用户那里收集一个数字,并显示它,当输入新数字时,显示所有输入的数字。但每次我输入一个新号码时,它只会删除过去的号码,只显示输入的新号码。有什么帮助吗?

def main():



def choiceTypeNumber():
global number
number = input("Please type in a number -> ")
print("Thank you for your number, now returning to menu.n")
menu()
return number

def choiceDisplayAll():
global number
print(number)
menu()

def choiceQuit():
print("Goodbye!")
exit()

def menu():
number = []
menu = ["G] Get a number", "S] Display current sum", "A] Display current average",
"H] Display the current highest numer", "L] Display the current lowest number",
"D] Display all numbers entered", "Q] Quit"]

print("##########################n#Welcome to the program! # n#Please input your choice#n##########################n")
for item in menu:
print(item)
choice = input()
if(choice.upper()== "G"):
choiceTypeNumber()

elif(choice.upper()== "D"):
choiceDisplayAll()

elif(choice.upper() == "Q"):
choiceQuit()

menu()

main()

添加到列表的方法是.append。当您说number = input('...')时,您正在将一个新值重新分配给number。如果过度使用,全局语句也会使代码变得混乱。如果列表在其他函数之外,则可以在没有全局语句的情况下对其进行变异。

def main():
numbers = []  # no need to use globals
def choiceTypeNumber():
numbers.append(input("Please type in a number -> "))  # You were overwriting the list here. Use append to add to the list
print("Thank you for your number, now returning to menu.n")
menu()
def choiceDisplayAll():
for num in numbers:
print(num, end=' ')  # print each number
menu()
import numpy as np
class SomeClassName():
def __init__(self):
self.numbers = []
self.menu = ["G] Enter a new number", 
"S] Display current sum", 
"A] Display current average",
"H] Display the current highest numer", 
"L] Display the current lowest number",
"D] Display all numbers entered",
"M] Display the Menu"
"Q] Quit"]

self.menuInputOptions = ['G', 'S', 'A', 'H', 'L', 'D', 'M', 'Q']
self.quitFlag = True
self.valid = False

print ("##########################n#Welcome to the program!n##########################n")

self._displayMenu()

while self.quitFlag:
self.selectOption()


def _choiceTypeNumber(self):
num = input("Please type in a number -> ")
self.numbers.append(float(num))

def _displaySum(self):
print (f'The sum is {np.sum(self.numbers)}')

def _displayAverage(self):
print (f'The average is {np.mean(self.numbers)}')

def _displayMax(self):
print (f'The max is {np.max(self.numbers)}')

def _displayMin(self):
print (f'The min is {np.min(self.numbers)}')

def _displayAll(self):
print (f'The numbers are {self.numbers}')

def _quitProgram(self):
self.quitFlag = False

def _displayMenu(self):
for option in self.menu:
print (option)

def _displayEmptyError(self):
print ('This option is not valid because no numbers have been inserted')
self.selectOption()

def selectOption(self):
if self.numbers:
self.valid = True

choice = input('Please select an option n').upper()
if choice not in self.menuInputOptions:
print ('Entry not a valid option, please choose from the following:')
print (self.menuInputOptions)
self.selectOption()

if choice in ['S', 'A', 'H', 'L'] and not self.valid:
choice = None
self._displayEmptyError()

if choice == 'G':
self._choiceTypeNumber()
elif choice == 'S':
self._displaySum()
elif choice == 'A':
self._displayAverage()
elif choice == 'H':
self._displayMax()
elif choice == 'L':
self._displayMin()
elif choice == 'D':
self._displayAll()
elif choice ==  'M':
self._displayMenu()
elif choice == 'Q':
self._quitProgram()

test = SomeClassName()

相关内容

  • 没有找到相关文章

最新更新